Types of Memory in Java

Java is a popular language used for several purposes, however, it is the structure and architecture of this language that makes it so popular and efficient for multiple purposes. In this post, we will be discussing the types of memory in Java. Just to mention that this concept is not limited to Java, all languages have a similar division of memory. Thus, this tutorial would be valid for any Langauge (C/C++/Python, etc).

When we talk about memory in Java, the memory here simply means RAM. When we say types of memory we are talking about the logical representation of memory by Java. So, basically, we are talking about RAM only but Java divides it into parts and uses it for different purposes. You can learn how Java does this by exploring its architecture, JVM, JRE, and official docs. If you are not comfortable with reading you might check this video to learn the same in a visual and interactive manner. Without further ado let’s dive right into it.

What do we save in memory? πŸ€·β€β™€οΈ

This might be a lame question but I found people asking it. When a program is executed it is brought into RAM from the HDD or your normal storage device. Why? Because the RAM read/write speeds are way better than HDD/SDD, and the processor is even faster. So to sync better and make the execution fast, we basically load currently executing files into the RAM. Further, it’s the job of the program to use the memory as it wishes.

TLDR: Your complete program is loaded into memory, it is executed while it's in memory. Thus the effort to explore types of memory.

Types of memory πŸ”’

1. Stack Memory 🏨

This is a temporary memory that stores the Stack Trace or function calls. All the function scope variables are stored here. As the name suggests it’s like the Stack Data Structure, where function calls are entered and removed from the same end. So, it follows the Last In First Out approach. Remember that Stack Memory is limited and that’s the reason why we often see StackOverflow errors when you end in a never-ending loop or recursive tree. Currently executing function enters the Stack and when it finishes execution it exits the stack, that’s why we said temporary memory above.

Note: Only Primitive Variables are stored in Stack Memory, Non-Primitive types are stored in heap memory. Also Static and Instance Primitive variables are also stored in Heap Memory.

2. Heap Memory πŸ—»

This Region of memory is used for dynamic allocation. It allows the manual allocation and deallocation of memory blocks of variable size and lifetime. In stack memory, function calls are automatically inserted and you never have to actually interact with the Stack Memory. Meanwhile, heap memory is used for dynamic allocation which has to be done by the developer. Unlike Stack Memory, this is not fixed it can expand and shrink according to the needs of the program and is decided by JVM. It can basically expand the Heap memory till there is no RAM available on the system.

What is stored where?

1. Function Calls

As I mentioned above without any doubt Function Calls are stored in Stack Memory, along with the local and primitive variables declared within the function, emphasizing the local word (more on this ahead).

void fun(){ 
  int a = 10; // Primitive and Local βœ… Stored in Stack Memory

2. Primitive Variables

If you don’t know what are primitive variables, they are basically variables with predefined sizes. Like int, float, char etc are all primitives. However as easy as it seems it is not, moreover where we store it in memory depends heavily on where we define it. Let’s see different ways where we may define a primitive.

1. Local Primitive Variables

We have discussed this scenario, quite enough but the x and a are local variables to the function, and thus will be stored in the stack trace of func itself.

var func(int x){ // Also a local variable in function func, Stored in Stack βœ…
   int a = 10; // Locally declared variable, Stored in Stack βœ…

2. Static Primitive Variables

If you might be familiar with static variables, they are not specific to an instance they are loaded by ClassLoader in Java beforehand. Meaning to say all objects of class Example will point to a single variable x and not different variables. Thus every stack will have a variable, but it will be referencing an address in Heap.

If still, it’s not clear you may want to check my whiteboard explanation which may help you with the intuition.

class Example{
   static int x = 10; // Static Primitive, not stored in Stack ❌
   void fun(){...}

3. Instance Primitive Variables

Variables declared within the class scope are referred to as Instance variables, unlike static variables it’s unique to each object. This type of variable is also stored in Heap Memory.

class Example{
   int x = 10; // Instance Primitive variable, not stored in Stack ❌
   void fun(){...}

3. Non-Primitive Variables

These types of variables do not hold values by themselves but point to some address, and note that they point to an address in heap. Thus it’s self-explanatory it’s stored in Heap. Some examples of Non-Primitive Data Types are Integer, String, Double, etc.

Objects are always stored in heap memory irrespective of where they are declared.

Conclusion πŸŽ‹

That encapsulates our discussion on types of memory in Java. We looked into Heap and Stack memory and also looked at what is stored where. I hope this blog was of some use to you, if you liked it or have any suggestions comment down below. Have a nice day, Peace.

For people still hanging out I would like to mention that we write some detailed blogs on Flutter topics and if you are interested you can check these out:

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Experience of my First Hackathon: MLH #HackFit2

Next Post
19 powerful ways to use the ls command in Linux

19 Powerful Ways to Use the ls Command in Linux