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.
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.
What is Concurrent Mark and Sweep collector>
It is referred to as concurrent low pause collector. CMS works on Old generation and algorithm for new generation is same as parallel collector. Suitable for responsive applications which cannot afford high pause times.
G1 garbage collector or “Garbage first” collector?
Available from Java 7, to replace CMS in long run. It is parallel, concurrent and incrementally low pause collector. There is no concept of young or old gen. It divides heap in multiple equal regions. It first collects regions with least live data, so “Garbage first”
- Meant for multiprocessor machines with large memories
Why is G1 called Garbage first?
Because it divides heap into equal sized regions. When GC is invoked it collects region with lesser live objects, so it is called garbage first.
-XX:+UseSerialGC
Activate serial GC
-XX:+UseParallelGC
Activate parallel new GC
-XX:+UseParallelOldGC
Activate old parallel GC which uses parallel threads for both old and new collection.
-XX:+UseConcMarkSweepGC
Activate CMS collector
-XX:ParallelGCThreads=N
Number of threads used by Parallel GC
-XX:+UseG1GC
Activate G1 GC
–XX:ParallelCMSThreads=n
Number of CMS threads
How to detect memory leak?
Overall memory utilization is increasing continuously and memory is not reaching to base level even after garbage collection.
What is churn rate?
Rate at which the application is allocating new objects. Number of young generation collections provides information on it. The higher the number, the more the churn rate.
What issue arrises with high churn rate?
It negatively impacts response time because minor GC is triggered frequently. Also the old gen fills quickly because young generation cannot cope with quantity of objects.
What is GC pressure?
GC pressure occurs when churn rate is high and objects are pre maturely tenured. This indicates sizing issue of heap or too high churn rate.
What indicates there is GC pressure or Heap sizing issue?
Old generation fluctuates greatly, then objects are being copied unnecessarily from young generation. Either young is too small, churn rate is too high.
jstat utility
For diagnosing performance issues with GC, heap sizing. Does not need any flags to be enabled. Included in JDK by default.
jstat -gc
jmap -heap
Provides heap information
- Information specific to GC algorithm, threads
- Heap configuration provided at command line
- Heap usage, capacity, free. Region wise details are provided.
jmap -histo
TBA