JVM Parameters

Introduction to JVM Parameters

In this post, we will be covering some of the most important and critical JVM Parameters which can be used to configure and fine tune JVM.

Performace of the JVM depends on the factor as to how well JVM is configured for a given application. Let’s cover few of the well known JVM parameters to start with. 

 

This post will serve as a starting point to understand some of the key parameters for the JVM, this post does not cover advanced JVM optimization techniques.

 

1. Heap Memory

One of the main optimization features involves assigning correct heap size to the JVM based on our application. It is recommended to specify minimum and maximum heap size to the JVM

1.1 -Xmxsize (Maximum Heap Size)

Xmx is used to specify the maximum size of the memory, to specify maximum heap size, we can use the following convention

-Xmx<Heap Size>[Unit]

HeapSize denotes the size of the heap and should be multiple of 1024.Unit indicates the unit of the memory in which we want to specify to the JVM.Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

For example, if we want to assign maximum of 6GB to our JVM, all of the following parameters are valid

-Xmx6442450944
 -Xmx6000m
 -Xmx6G

 

1.2 -Xmssize (Minimum Heap Size)

Xms is used to specify the minimum size of the memory, to specify minimum heap size, we can use the following convention

-Xms<Heap Size>[Unit]

HeapSize denotes the size of the heap and should be multiple of 1024.Unit indicates the unit of the memory in which we want to specify to the JVM.Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

For example, if we want to assign minimum of 4GB to our JVM, all of the following parameters are valid

-Xms4294967296
 -Xms4000m
 -Xms4G

 

For assigning minimum 4GB and maximum 6GB to JVM, we can use following parameters.

-Xms4G -Xmx6G

 

1.3  -XX:NewSize=size (Young Generation Heap Size)

To understand this parameter, we need to understand about young generation region in the heap, young generation region of the heap is used for new objects and due to it, this is known as one of the most important JVM parameters apart from -Xmx and -Xms.

Keep in mind that GC (Garbage collection) is performed more often in this region of the memory, so this needs special consideration as if the size is too low, then JVM will perform a large number of minor GC operation and if size is too large a full GC will be performed which can be time-consuming, this entire activity can have a major impact on the application based on the configured GC. 

We can use the following convention to set size for young generation

-XX:NewSize=<young size>[k|m|G]

For example, to set initial size of the young generation to 1GB, we can use one of the following conventions

-XX:NewSize=1024m
 -XX:NewSize=1G 

We can use -XX:MaxNewSize=size to set a maximum size of the heap for the young generation.

Following JVM parameters has been removed from Java 8, setting those parameters will not perform any modification or optimization to the JVM. It is recommended not to use these parameters -XX:MaxPermSize=size
-XX:PermSize=size

 

2. Garbage Collection Options

Garbage collector can play a vital role in your application and choose a correct Garbage Collector (GC) is really important. JVM provide following type of Gargabe collector

  • Serial Garbage Collector (XX:+UseSerialGC)
  • Parallel Garbage Collector (XX:+UseParallelGC)
  • CMS Garbage Collector (XX:+USeParNewGC)
  • G1 Garbage Collector (XX:+USeG1GC)

Please read  Java Garbage Collector for more details.

 

3. Java Garbage Collection Log (GC Logging)

To debug memory issues or application performance, We can enable GC logging in JVM. GC log provides a lot of information and statics about the Garbage collection process, some of the information (but not limited) provided by GC logging are

  • How Long GC Run
  • Information about when full GC run.
  • Information about the JVM pause when GC was running.
  • Information about Young and Old generation regions.

To understand GC logging, we need to enable GC log in the JVM

3.1 -XX:+PrintGCDetails (Print GC details Messages)

This option is disabled by default, we need to use this flag to enable printing detail messages at every GC.

3.2 -XX:+PrintGCDateStamps (Print GC details Messages)

Can be used for the printing of a date stamp at every GC, this option is also disabled by default.

3.3 -Xloggc:filename

This can be used to set file name and location to which GC output will be redirected.

3.4  -XX:+UseGCLogFileRotation

Should be used to enable log rotation.This can be really helpful for handling large log files produced by long running Java Applications.This property must be used with -Xloggc:<filename>.

3.5  -XX:NumberOfGClogFiles=<number of files>

Sets the number of files to use when rotating logs, default value is 1 and it should be always >=1

3.6 -XX:GCLogFileSize=<number>M (or K)

Defines the size of the log file at which point the log will be rotated, default will be set to 512K

Here is an example with above configurations

-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-Xloggc:/home/DATA/gc.log
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=5
-XX:GCLogFileSize=20M

Please note that using  +PrintGCDateStamps can add some overhead since an additional thread will be required to monitor system time.

 

4. -Dproperty=value (32 /64 bit)

Sets a system property value. The property variable is a string with no spaces that represents the name of the property.To run an application on 32 or a 64-bit environment, we can use this property as

-d<OS bit>

e.g. -d32 for 32-bit environment or -d64 for 64-bit environment.

 

5. Additional Properties

-server : Selects the Java HotSpot Server VM.For 64 bit, this option is implicit.

-XX:MaxHeapFreeRatio=percent : Sets the maximum allowed percentage of free heap space after a GC event (e.g. -XX:MaxHeapFreeRatio=75 for setting maximum free heap ratio to 75%:)

-XX:MaxMetaspaceSize=size : Sets the maximum amount of native memory that can be allocated for class metadata, there is no limit by default and it will be dependent upon the size/type of application. (-XX:MaxMetaspaceSize=512m to set maximum class meta data size to512 MB)

XX:SurvivorRatio=ratio : Sets the ratio between Eden space size and survivor space size.

 

Summary

In this post, we covered some of the main JVM Parameters which can be used to optimize and configure JVM based on our application. As mentioned in the beginning this post just outlines few of the important JVM Parameters. Please refer to Java HotSpot VM Options for more details (Please refer to correct JDK version for updated list).