Java Garbage Collector Tuning and Selection
2024-02-10
Index
- Monitoring Garbage collections
- Turning off automated heap allocation sizing
- Tuning garbage collection
- Selecting a garbage collector
- G1 garbage collector
- Tuning the G1 garbage collector
- String de-duplication
- Misc
Monitoring Garbage collections
We can pass -verbose:gc
VM argument to monitor the garbage collection. If it shows simple GC then it is minor garbage collection.
If it shows GC full then it is major garbage collection.
Turning off automated heap allocation sizing
Java’s heap Eden
, S0
, S1
and Old generation
are allocated at runtime and these size can very. To stop this behavior we will have to disable the flag.
We can do so by passing following VM argument.
-XX:-UseAdaptiveSizePolicy
Tuning garbage collection
Old and Young allocation
We can specify how much be the size of our old generation should be w.r.t young generation. We can use following flag.
-XX:NewRatio=3
This means old generation will be 3 times of young generation.
Survivor space allocation
We can specify size of survivor space using following flag.
-XX:SurvivorRatio=8
This means that survivor space will be 1/8th of the young generation. Since we have 2 survivor spaces, 2/8th will be used for survivor and remaining 6/8th will be occupied by Eden.
Generation needed to become old
We need to set below mentioned flag, this number value tells that an object need to survive following number of times in young generation GC process. Once it is completed then it is moved to old generation.
-XX:MaxTenuringThreshold=3
Maximum can be 15 only. JVM automatic decide this number for us by default
Selecting a garbage collector
Types
- Serial - Pause the application and then run on single thread.
(-XX:+UseSerialGC)
- Parallel - Can run parallel
(-XX:+UseParallelGC)
Default since java 8 - Mostly Concurrent - These are of two types
- Concurrent Mark Sweep GC
(-XX:+UseConcMarkSweepGC)
Default since java 9 - G1 GC
(-XX:+UseG1GC)
Default since java 10
G1 garbage collector
This is most optimal garbage collector. In this heap is split into multiple spaces and eden
, s0
, s2
and old
are blocks are distributed randomly. When GC is performed on eden
and objects are surviving then it mark that particular block as old
and allocate new memory block to eden
.
Tuning the G1 garbage collector
- Important flags
-XX:ConcGCThreads=n
It tells the number of thread to be used by GC to cleanup space in young generation.-XX: InitiatingHeapOccupancyPercent=n
G1 run GC process when eden is at least 45% full and same applies to other spaces as well. We can change the number by using this flag.
String de-duplication
This is only available in G1. We will have to use following flag.
-XX:UseStringDeDuplication
When we have so many string duplicates inside the heap then during GC, G1 will compare the string hash and it will change the reference to single string and rest will be garbage collected. This is helpful when we have so many string duplicates.
Misc
Viewing running java processes and PID
We can use JPS
command in terminal to check java running processes and PID.
Viewing enabled flag on a process
We can use following command to check if flag is enabled or not.
jinfo -flag UseAdaptiveSizePolicy <PID>