JVM Memory Managemnt and Garbage Collection Flashcards
What does it mean by generational collectors?
Most objects in an application are short lived. So to avoid scanning whole heap to clear objects it is better to divide heap in sections based on Generations. Young and Old.
Young Gen sections?
Eden space, where objects are first allocated. Survivor spaces, S0 and S1.
What is old gen also called?
Tenured area. Objects that have survived some number of GC cycles are probable to live long, so are tenured in old gen area.
What is GC that occurs in Young gen called?
Minor GC
What is GC that occurs in Old gen called?
Major GC or Stop the world GC.
Heap structure
Young Gen (Eden + Survivor spaces), Old Gen, Perm Gen (Method area)
When does Minor GC occur?
When Eden space gets full.
What does Minor GC do?
Collects garbage from Young gen. All objects that are referenced are moved to one of the survivor spaces. Minor GC also checks for survivor objects in survivor spaces and moves them to other survivor space. So at any given point in time, one survivor space is empty.
When are objects moved to Old gen?
When objects have survived many minor GC cycles, they are moved to old gen space. This is triggered either by threshold or age of objects.
When does Major GC occur?
When old gen is getting full. It is triggered using some ratio of old gen. Say trigger major GC if 60% gets full.
Which is faster Minor GC or Major GC?
Minor is faster.
What is keep area?
It contains most recently allocated objects and is not garbage collected until the next GC cycle. This is done to prevent objects from being promoted just because they were allocated just before young collection started.
What is metaspace?
In Java 8, there is no perm gen. Unlike Perm gen which is part of heap, Metaspace is not part of heap. Allocation for class metadata is done out of native memory. Metaspace grows in size while PermGen was fixed in size.
PermGen vs Metaspace?
- PermGen till Java 7 and Metaspace from Java 8.
- PermGen is fixed in size while Metaspace can grow
- PermGen is part of heap while Metaspace is not part of heap, it is allocated from native memory.
What is code cache?
Code cache is the area where JIT compiled native code is cached. This area is flushed if its size exceeds given threshold.
-Xms flag
Set the initial size of heap when JVM starts
-Xmx flag
Set the maximum size of heap
-Xmn flag
For setting size of Young gen, rest of the space goes to old gen
-XX:PermGen flag
Initial size of Perm Gen
-XX:MaxPermGen flag
Maximum size of Perm Gen
-XX:SurvivorRatio flag
Ratio of Eden space to survivor. Eg if 10m is size of young gen and survivor ratio is 2, then 5m is allocated to young and 2.5m for each survivor space. Default value is 8
-XX:NewRatio
Ratio of old/new gen size. Default value is 2
Which are the stages of garbage collection
- Marking phase
- Sweeping phase
- Compaction phase optional
What is mark phase?
Mark phase is process of finding which objects are reachable and are marked as live. Marking phase starts from GC roots such as Java threads, native handlers etc.