Difference between Stack vs Heap in Java
- Get link
- X
- Other Apps
What Is Stack?

Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.
What Is Heap?
The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.
The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.
As we know objects are created inside heap memory and Garbage collection is a process which removes dead objects from Java Heap space and returns memory back to Heap in Java. For the sake of Garbage collection Heap is divided into three main regions named as New Generation, Old or Tenured Generation and Perm space. New Generation of Java Heap is part of Java Heap memory where newly created object are stored, During the course of application many objects created and died but those remain live they got moved to Old or Tenured Generation by Java Garbage collector thread on full garbage collection. Perm space of Java Heap is where JVM stores Meta data about classes and methods, String pool and Class level details. You can see How Garbage collection works in Java for more information on Heap in Java and Garbage collection.
The heap mainly store objects create using or class level variables.
Difference between Stack vs Heap in Java
Pseudo Code to create an object on the stack:
void somefunction() { /* create an object "m" of class Member this will be put on the stack since the "new" keyword is not used, and we are creating the object inside a function */ int a = 10; } //the variable "a" is destroyed once the function ends
Pseudo Code to create an object on the heap:
void somefunction( ){ /* create an object "m" of class Member this will be put on the heap since the "new" keyword is used, and we are creating the object inside a function */ Member m = new Member( ) ; /* the object "m" must be deleted otherwise a memory leak occurs */ m = null; }
Word of Advise for Developer to Avoid Memory related errors:
OutOfMemoryException :
(1) Avoid creating new objects for smaller operations: Use primitive types for calculation instead of wrapper classes. For strings you can create String s = new String("myString")-> this will create new string object instead you can utilize string pool where same string object is returned : String s = "myString";
(2) For large size projects: where database returns big result set for select query: we can use batch select instead of big result set having all rows.
(3) Programmaticaly call System.gc() --> regular interval
(4) Use Sonar/Jprofiler to check memory leaks
StackOverflaw:
(1) Avoid cicular calling of methods (recursive call) which has big set of variables defined.
(2) Instead of creating custom class having many properties, we can have different object with less number of properties
(3) Follow SOLID (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion) principle of class design ( http://en.wikipedia.org/wiki/SOLID_(object-oriented_design))
There can be many more suggestions based on application needs but above are common guidelines.
- Get link
- X
- Other Apps
