Essentials JVM Options

Hi Folks,

This article summirizes few most important JVM options or JVM arguments that can be used to monitor/debugging purposes or essential running of java application in production.

  1. Setting Java Heap Size
  2. Garbage Collection
  3. Garbage Collection Logging
  4. Set Free Heap Ratio
  5. Handling out of memory error
  6. Tracing of Classloader
  7. Setting TimeZone
  1. Setting Java Heap Size: This is widely used and most important JVM argument.
 -Xms<heap size>[unit] : set initial Java heap size
 -Xmx<heap size>[unit] : set maximum Java heap size

The unit can be specified in GB, MB or KB. Most of time these two argument used together.

e.g. -Xms2G -Xmx5G

It means at startup, 2GB menory allocated to JVM and maximum it can be 5GB if required. Before Java 8, permGenSpace is being used to define the string pool and class , method metadata. But from Java 8 and onwards this been discarded and replace by MetaSpace.

Metaspace is the region where JVM’s metadata definitions, such as class definitions, method definitions, will be stored. There is no such upperlimit defined for this but to avoid
unnessesary memory allocation one can limit it by as follows:

-XX:MaxMetaspaceSize=[unit] e.g. -XX:MaxMetaspaceSize=5G

2. Garbage Collection:

Basically, it is tracking down all the objects that are still used and marks the rest as garbage. Such objects are reclaimed to free memory.
In Java 8 Parallel GC is the default GC algorithm and since Java 9, G1 GC is the default GC algorithm. Rest available GC are as below

-XX:+UseSerialGC : Serial Garbage Collector
-XX:+UseParallelGC : Parallel Garbage Collector
-XX:+UseG1GC : G1 Garbage Collector
-XX:+UseZGC : Z GC.
-XX:+UseEpsilonGC : Epsilon GC

3. Garbage Collection Logging : Garbage Collection logs contain information about Garbage Collection events, latency, memory reclaimed, pause time duration, etc.
Enable GC log by passing following JVM arguments:

For JDK 8: -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:{file-path}

e.g. -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/var/log/myAppGc.log

for JDK 9+: -Xlog:gc*:file={file-path}

e.g. -Xlog:gc*:file=/var/log/myAppGc.log

GC logs used to tune garnage collection performance.Higer the GC Throughput better is the application performance. Suppose your application has been running for 60 minutes.
In this 60 minutes, 2 minutes is spent on GC activities.

It means application has spent 3.33% on GC activities (i.e. (2 / 60) * 100). It means Garbage Collection throughput is 96.67% (i.e. 100 – 3.33).
Like wise GC logs used to check Object Creation Time, Peak Heap Size etc.

4. Set Free Heap Ratio: To avoid unnecessary shrinking or expansion of Heap Space. Following arguments used to set the free heap ratio

-XX:MinHeapFreeRatio: sets the minimum percentage of heap free after GC to avoid expansion.
-XX:MaxHeapFreeRatio: sets the maximum percentage of heap free after GC to avoid shrinking

5. Handling out of memory error: Mostly in multithereaded applications or higly memory sensitive apps, application crashesh after throwing OutOfMemoryError.
To diagnose OutOfMemoryError or any memory-related problems, one would have to capture the heap dump right at the moment

-XX:OnOutOfMemoryError="shutdown -r" : used to specify action when OutOfMemoryError occurs.

6. Tracing of Classloader: Tracing loading and unloading of classes if importannt to find out ant classloader memory leakage.It can be enabled as below


7. Setting TimeZone: This is most useful specially where the data stored or acceesed based on Time/Zone. like trading apps or Schedule jobs.

By default, date and time objects pick timezone onformation from underlying OS. In case of distributed systesms where servers are located across the globe or two different timezones,
the JVM picks repective timezone values and so it differs.


To avoid this, its best to set timezone at JVM level using -Duser.timezone system property as below


I hope you find these JVM arguments useful and handy.

Stay tuned.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s