The Java Virtual Machine defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits. The two main area are Heap and Stack area.
- Heap ->The heap is the run-time data area from which memory for all class instances and arrays are allocated. Whenever we create any object, it’s always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application (global access). Heap memory lives for the entire duration of the application execution.
-
Stack->Each thread has a private Java Virtual Machine stack. A Java Virtual Machine stack stores frames which holds local variables and partial results, and plays a part in method invocation and return. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order (figure n.1). Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method. Stack memory size is very less compared to Heap memory and because of of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory. Please note that:
-
If the computation in a thread requires a larger Java Virtual Machine stack than is permitted during JVM startup, the JVM throws a StackOverflowError.
-
If JVM stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial JVM stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.
-
In the following code, numberObj and memoryTest are created on the heap while in the stack there are the references to them.
package education.jtrainer; public class MemoryTest1 { public static void main(String[] args) { int number=10; // this is created in the Stack Integer numberObj=new Integer(10); // this is created in the Heap, numberObj reference is kept in the stack MemoryTest1 memoryTest = new MemoryTest1(); // this is created in the Heap, mem reference is kept in the stack memoryTest.print(numberObj); // a block in the top of the stack is created to be used by print(numberObj) method. Since Java is pass by value, a new reference to Object is created in the print(numberObj) stack block // at this point stack memory block allocated for print(numberObj) in stack becomes free. // at this point stack memory block created for main() method is destroyed. Also the program ends at this line, hence Java Runtime frees all the memory and end the execution of the program. } private void print(Integer param) { //param is a reference to an object on the heap System.out.println(param); } }
The picture below shows how the memory is allocated.
Figure n.1: stack memory vs heap memory
References
[1] – https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html
[2] – https://www.journaldev.com/4098/java-heap-space-vs-stack-memory
[3] – http://kumarsoablog.blogspot.it/2013/02/jvm-parameter-survivorratio_7.html