Flashcards in JVM Memory Managemnt and Garbage Collection Deck (118)
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?
What is GC that occurs in Old gen called?
Major GC or Stop the world GC.
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.
Set the initial size of heap when JVM starts
Set the maximum size of heap
For setting size of Young gen, rest of the space goes to old gen
Initial size of Perm Gen
Maximum size of Perm Gen
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
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.
What is sweep phase?
In sweep phase, the heap is traversed to find gaps between live objects and gaps are recorded in freel list. That space is then available for new object allocation.
What is heap compaction?
It is similar to disk defragmentation. When there are small holes between objects, the performance of object allocation suffers. So during compaction all objects are moved together to create contiguous section of used and free memory.
What is Serial GC?
Uses simple mark-sweep-compact approach for young and old generation. Uses only single thread.
What is Parallel GC?
Uses N parallel threads to do Young gen collection and serially in Old generation. N is equal to number of CPU cores in system.
What is Parallel Old GC?
Same as Parallel new GC but it uses multiple threads for both young and old collection.