Java Garbage Collector Tuning and Selection

2024-02-10

Index


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.

verbose gc

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

  1. Serial - Pause the application and then run on single thread. (-XX:+UseSerialGC)
  2. Parallel - Can run parallel (-XX:+UseParallelGC) Default since java 8
  3. 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 oldare 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
  1. -XX:ConcGCThreads=n It tells the number of thread to be used by GC to cleanup space in young generation.
  2. -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>


More posts like this

Java Garbage Collection

2024-02-09 | #Garbage Collection #Java #JVM tunning #Performance & optimization

Index Garbage Collection System.gc() Changing size of Heap Soft Leaks Heap dump Generating heap dump Viewing heap dump Generational Garbage Collector Introduction Categories of Young Generation Viewing GC generations in VisualVM Garbage Collection Garbage collection in Java is a mechanism that automatically manages the memory used by a Java program (process to free up memory).

Continue reading 


Hello World

2023-05-23 | #AWS #Java #Leetcode #Node.js

Hello World, I started this blog to share my daily learning progress. I love to solve daily Leetcode challenges. I use Java, Nodejs and AWS for backend development. You can find more about me in About section.

Continue reading 