Java Memory Model – Explained
Java memory model defines how the JVM works in our system. It is vital to understand how the Java memory model works to write the programs. Let us see in details about Java memory model
Difference between Heap and Stack
Heap Memory |
Stack Memory |
Store objects |
Store local variables and function call |
Has the actual object |
Will have reference to the objects in the Heap Memory |
OutOfMemory Error |
StackOverFlow error when stack is full |
All thread can access all objects in the Heap |
Only owner thread can access the variables in the Stack |
Young Generation:
The young generation is the place where all the new objects are created. When the young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts – Eden Memory and two Survivor Memory spaces.
Important Points:
Most of the newly created objects are located in the Eden memory space.
-
When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
-
Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.
-
Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually, By setting an age for the young generation objects before they become eligible to promote to Old generation.
PermGen space:
Stores class related data, and
is used to keep information for loaded classes and few other advanced features like StringPool.
MetaSpace: (From 1.8, instead of perm gen)
- Are part of native memory – OS level
- Metaspace by default increases its size , To the limit of OS, while PermGen always has a fixed maximum size.
- No more out of memory error
- We can set the size of metaspace and also it increases automatically, but permgen wont increase by itself
- Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the MaxMetaspaceSize.
So where does static variables and static classes are stored? Is it heap or Permgen?
- Since the heap has actual objects, static variables and static classes are stored in Permgen / Metaspace.
Eden and Survivor Space:
- Most of the newly created objects are located in the Eden memory space.
- When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
- Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.
- Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually, it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation.
Types of Garbage Collectors:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
Serial Garbage Collector:
A single thread is used for garbage collection and it will freeze all the application threads while performing GC (Garbage Collection).
Parallel Garbage Collection:
It uses multiple threads for garbage collection, this also freezes all the application threads while performing GC
CMS Garbage Collector:
Concurrent Marksweep Garbage collector, CMS scans the heap mark and mark the instances that requires to be removed and then will clear the marked instances
G1 Garbage Collector:
This will divide the heap memory into equal parts and will perform the GC on the part which has lesser live data.
What is the difference between CMS and G1GC?
Both are designed to minimize long pause when performing GC. But G1GC is used for larger heap that is more than 4GB.
CMS – Uses one or more thread to scan the memory periodically and remove the unused objects, where pause time is minimal but cpu time is more
G1GC – It divides the memory into equal parts and cleans old generation by copy from one part to another.