JVM parameters are used to configure the Java Virtual Machine(JVM). The performance of JVM depends on how well it is configured to meet the needs of the Java application.
In this tutorial, we will learn how to set some of the JVM parameters.
We can set the minimum and maximum heap size required by an application with the help of the Xmx and Xms parameters. Setting the correct heap size can affect the performance of our application. Xmx is used to adjust the maximum heap size, and Xms is used to change the minimum heap size.
The unit indicates the memory unit like 'g' for GB, 'm' for MB, and so on.
The Young Generation memory region inside the heap is used to store the newly created objects. We can also alter the initial and maximum size of the Young Generation. XX:NewSize adjusts the initial size, and XX:MaxNewSize sets the maximum size.
Choosing an appropriate garbage collector for our application is also crucial for its performance. JVM provides four different garbage collection implementations.
Serial Garbage Collector
Parallel Garbage Collector
CMS Garbage Collector
G1 Garbage Collector
Java Garbage Collection Logging
GC logging is used to keep track of the application's health and performance. We can log the garbage collection activities by using the following parameters.
-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=<number of log files> -XX:GCLogFileSize=<file size><unit> -Xloggc:[path to gc.log file]
- UseGCLogFileRotation is used to enable log rotation. It is often used to handle large log files for long-running applications.
- NumberOfGCLogFiles adjusts the maximum number of log files to use when rotating logs. Its default value is 1.
- GCLogFileSize defines the maximum size of the log file.
- loggc denotes the location of the log file created.
The Java Virtual Machine automatically chooses the 32-bit environment if 32-bit and 64-bit packages are present. We can change from 32-bit to 64-bit by using the following parameter.
Out Of Memory
The Out Of Memory error occurs when the JVM cannot allocate memory to new objects, and the garbage collector cannot clear any space. We can use some JVM parameters to capture the heap dump in a file.
- HeapDumpOnOutOfMemoryError notifies the JVM to capture the heap dump in a file.
- HeapDumpPath indicates the path of the file where we want to capture the heap dump.
- OnOutOfMemoryError sets the action to be performed when this error is encountered.
- UseGCOverheadLimit sets a limit on the time that the JVM can spend on garbage collection.
Other Frequently Used Parameters of JVM
- -server enables the Java HotSpot Server VM. Note that for 64-bit environments, this option is used by default.
- -XX:+UseStringDeduplication optimizes the memory used by storing duplicate strings by maintaining a single global char array.
- -XX:+UseLWPSynchronization uses Light Weight Process Synchronization(LWP) instead of normal thread synchronization.
- -XX:MaxHeapFreeRatio sets the maximum ratio of free heap space after the garbage collection. It avoids shrinkage.
- -XX:MinHeapFreeRatio sets the minimum ratio of free heap space after garbage collection. It avoids expansion.
- -XX:SurvivorRatio sets the ratio of the eden to survivor space.
- -XX:+UseStringCache allows the caching of the most common strings in the string pool.
- -XX:+UseCompressedStrings allows the use of a byte array to store Strings in pure ASCII format.
- -XX:+OptimizeStringConcat optimizes the concatenation operation on Strings.
In this tutorial, we learned about some of the key JVM parameters. These parameters configure the Java Virtual Machine and improve the overall performance of our application. We can also use them for debugging purposes.