Last Updated:

What is Heap and Stack memory in Java?

Java Heap Memory

Java Heap (heap) is used to allocate memory for objects and JRE classes. Creating a new object also occurs on the heap. This is where the garbage collector works: it frees up memory by deleting objects that are not referenced. Any object created on the heap has global access and can be referenced from any part of the application.Java Runtime

Stack memory in Java

Stack memory in Java works according to the LIFO (Last-In-First-Out) scheme. Whenever a method is called, a new block is created in the stack's memory that contains primitives and references to other objects in the method. Once the method finishes, the block is also no longer in use, thereby granting access to the next method.
The stack memory size is much smaller than the amount of memory on the heap.

Let's look at the differences between stack memory and heap using a simple program as an example.

The picture below shows the stack and heap memory for the program above

java heap stack

Now consider the steps of our program:

  1. Once we run the program, all the runtime classes are loaded into a heap. The method then finds and the Java Runtime creates the stack memory for use by the main() method.main()строку 1
  2. Next, an int variable is created, which is stored in the memory of the stack of the .строке 2main()
  3. Then we created an object in and it immediately appears on the heap, and the stack memory contains a reference to it. Exactly the same process occurs when we create a Memory object in the .строке 3строке 4
  4. Now in we call a method and immediately create a block on top of the stack that will be used by this method. Because Java passes objects and primitives by value, a new reference to the object created on line 3 will be created in Java.строке 5exMethod()строке 6
  5. The string created in , is sent to the String Pool, which is located in the heap. This line is also referenced in the stack memory of the .строке 7exMethod()
  6. The method terminates on , so the stack block for this method becomes free.exMethod()строке 8
  7. The method terminates, so the stack memory for the method will be destroyed. Also, the program ends in this line, therefore, the Java Runtime releases all memory and terminates the program.строке 9main()main()

Difference Between Stack and Heap Memory in Java

Based on the above explanations, we can easily summarize the following differences between Heap and Stack memory in Java.

  • The heap is used by all parts of the application while the stack is used by only one thread of program execution.
  • Whenever an object is created, it is always stored on the heap, and the stack memory contains a reference to it. Stack memory contains only local variables of primitive types and references to objects on the heap.
  • Objects on the heap are accessible from any point in the program, while stack memory cannot be accessed by other threads.
  • Memory management in the stack is carried out according to the scheme .LIFO
  • Stack memory exists only for a while during the program's operation, and the memory in the heap lives from the very beginning to the end of the program.
  • We can also use the JVM options to determine the initial and maximum memory size in the heap. For a stack, you can determine the memory size using the .-Xms-Xmx-Xss
  • If the stack memory is fully occupied, the Java Runtime throws , and if the heap memory is full, an exception is thrown.java.lang.StackOverflowErrorjava.lang.OutOfMemoryError: Java Heap Space
  • The stack memory size is much smaller than the memory on the heap. Because of the ease of memory allocation(s), stack memory is much faster than the heap.LIFO

That's all you need to know about Stack and Heap memory in Java. Stay tuned for updates in the Utilities section.