Saturday, December 8, 2018

Stack Vs Heap memory in JVM



Basics of Java Memory

Let’s say you have a program running in the server/computer. Then your program need to access your server/computer’s memory.
Why do we need access to the memory?
You program may have variables to assign the data or creating few objects which needs to be stored.
In Java, the memory has been divided into two different categories.
1.       Stack
2.       Heap
JVM Memory Representation

Please note few important information.
·       In JVM, heap memory holds more memory than stack memory in size.
·       In JVM, there are many stacks and only one heap and all threads share the same heap. 
·       In multithread environment, every single thread has its own stack memory. Hence, data in the stack can be used by a thread that owns the stack. You can refer the below diagram.

let us talk about Stack.
What is stack memory?
1.       Stack is nothing, but a data structure managed by JVM. It means that java knows how to allocate the data in stack and when to deallocate them from the stack.
2.       Local variables such as integer, double will be stored in stack memory as their life will be short.
3.       Whenever function is called, the local variable of that function will be pushed on the stack.
4.       When you reach the end of program or end of code block, all local variables of the program/code will be destroyed from the stack and stack will be empty.
5.       Stack memory has been allocated by following first in last out algorithm (FILO). You will understand after seeing the below allocation/deallocation representation.
Allocating data in stack memory


Step # 1: First data (Satheesh) is allocated in to stack memory.
Step # 2: After the first data is allocated the stack memory will be like this.


Step # 3: Second data (Pandian) is allocated in to stack now.
Step # 4: Now the stack will be like below after adding the second data.
Each time when the new data added into the stack, the data which was added first will be pushed down to the bottom of the stack.
Deallocating data in stack memory
When the data scope is completed, it will be deallocated from stack memory. So, the last added data will be removed from the stack first and earlier data will be removed when it comes to the top of the stack.
Hope you understood what is stack and how the memory has been allocated to data in stack memory.
Now let us see what is heap memory.
·       Heap memory allows to store the data which has longer life than function/code block. For example, when you create an object and access through multiple function, the life time of object is long.
·       Objects are created and stored in heap memory and the newly created objected was referenced by a variable. But this variable will be stored in stack memory. 
For example, you are creating some variables in your program like below and let us see how the memory will be allocated in JVM for those variables.
int iLoop = 8;
Customer Satheesh = new Customer();
Customer object can be reference from other function/method as well.
That's all for now. Hope this article gives you some insight about JVM memory allocation.
Happy reading 😃









No comments:

Post a Comment

Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'. Again, we are using the sample file named &#...