Stack Vs Heap in Java

Thanusri swetha J October 22, 12:20 PM Technology

In Java, memory management is a vital process. It is managed by Java automatically. The JVM divides the memory into two parts: stack memory and heap memory. From the perspective of Java, both are important memory areas but both are used for different purposes. The major difference between Stack memory and heap memory is that the stack is used to store the order of method execution and local variables while the heap memory stores the objects and it uses dynamic memory allocation and deallocation. [1]

Figure 1. Stack Vs Heap Memory Allocation in Java

Figure 1 shows stack memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.

Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects. When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.

We can break this memory model down into smaller parts, called generations, which are:

  • Young Generation – this is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up.
  • Old or Tenured Generation – this is where long surviving objects are stored. When objects are stored in the Young Generation, a threshold for the object's age is set, and when that threshold is reached, the object is moved to the old generation.
  • Permanent Generation – this consists of JVM metadata for the runtime classes and application methods. [2]

Key Points:

  • It’s a temporary memory allocation scheme where the data members are accessible only if the method () that contained them is currently is running.
  • It allocates or de-allocates the memory automatically as soon as the corresponding method completes its execution.
  • We receive the corresponding error Java. lang. Stack Over Flow Error by JVM, If the stack memory is filled completely.
  • We receive the corresponding error message if Heap-space is entirely full, java. lang. Out of Memory Error by JVM.
  • This memory allocation scheme is different from the Stack-space allocation, here no automatic de-allocation feature is provided. We need to use a Garbage collector to remove the old unused objects in order to use the memory efficiently.
  • The processing time (Accessing time) of this memory is quite slow as compared to Stack-memory. [3]
References:
  1. https://www.javatpoint.com/stack-vs-heap-java
  2. https://www.baeldung.com/java-stack-heap
  3. https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/
Cite this article:

Thanusri swetha J (2021), Stack Vs Heap in Java, Anatechmaz, pp. 67

Recent Post

Blog Archive