Guide To The Most Important Java VM Options

May 09, 2022
Xblog JavaVM


Suppose you are a Java application server administrator or an experienced Java developer. In that case, you must understand what Java options are and how significant they are for a Java application. Even if you haven’t used a Java option, you must have heard about the importance of Java options. In this article, we will be exploring some of the essential Java options you can use to configure the Java Virtual Machine to improve the efficiency of your application.

Most important Java options you must know about

Following are some beneficial Java options to achieve various functionalities and improve your code’s performance. All these Java options are classified based on their primary functions. This will aid you in identifying their purpose.

new java job roles

Setting up the heap memory

JVM sets the heap memory by default, but to tweak its performance, it is common for Java developers to initialize the heap memory as per the requirement of their applications by using some JVM options.

You can specify the minimum and maximum heap size as per your requirements by using the following parameters:

-Xms<heap size>[unit] 

-Xmx<heap size>[unit]

Here, [unit] denotes the unit in which the memory is initialized. It can be marked as ‘k’ for KB, ‘m’ for MB, and ‘g’ for GB.

  • -Xms is used to define the starting heap size for JVM. E.g., Xms2048m would mean that an initial heap size of JVM is 2 GB.
  • -Xmx defines the maximum heap size of JVM, e.g., Xmx4096m which means the maximum heap size of JVM will be 4 GB.

-Xms and -Xmx are usually used together.

For instance, if you want to assign a range for the heap memory where the minimum memory is 512 MB and a maximum of 5 GB to your JVM, you need to write:

-Xms512M -Xmx5G

Setting up the percentage of the heap memory

Instead of setting a fixed size of heap memory, you can also set a percentage of the heap to be free after garbage collection (GC). The command mentioned below is used to set the maximum percentage of heap free after GC to avoid shrinking.


The Java option below sets the minimum percentage of heap free after GC to avoid going off the limit.


Setting the MetaSpace

Starting with Java 8, the developers never get to define the size of the MetaSpace memory as JVM automatically increases it as the global limit is reached, which causes instability. However, it can easily be overcome by setting a significant memory size for MetaSpace. This will then eliminate the need for increasing the size later. You can use the following option to set it as per your need and available memory in your system.

-XX:maxmetaspacesize=<metaspace size>[unit]

The <MetaSpace size> denotes the amount of memory you want to assign to MetaSpace, and the unit is the same as mentioned before: ‘ k’ for KB, ‘m’ for MB, and ‘g’ for GB.

Garbage collection algorithms

To achieve better stability for your application, selecting the correct garbage collection algorithm is a crucial step. We will not be discussing every algorithm, but it is essential to know that these algorithms are also implemented using Java options.

JVM offers a total of seven garbage collection algorithms. You need to specify the GC algorithm using the following JVM options explicitly, or JVM will use the default algorithm as your Java version. Until Java version 8, Parallel GC was the default garbage collection algorithm, but if you are using Java 9, G1 will be set by the JVM as the default algorithm.

Following are the seven algorithms and their respective Java options that you can use in your application:

Garbage Collection Algorithm JVM Parameters
Serial GC -XX:+UseSerialGC
Parallel GC -XX:+UseParallelGC
Concurrent Market & Sweep (CMS) GC -XX:+UseConcMarkSweepGC
G1 GC -XX:+UseG1GC
Shenandoah GC -XX:+UseShenandoahGC
Epsilon GC -XX:+UseEpsilonGC

Garbage collection logging

After selecting the correct garbage Collection algorithm, GC logging should be another step for your application. The GC logging is used to monitor the overall working of your application. The most straightforward approach is keeping track of GC by logging all the GC activities in the human-readable text form as it gets easier to identify if there is an issue.

The following parameters can be used to log the GC activity:

  • -XX:+PrintGCDetails
  • -XX:numberofgclogfiles=< Set the number of log files>
  • -XX:gclogfilesize=< file size >[ unit ]
  • -Xloggc:<file path>
  • -XX:+printgctimestamps
  • -XX:+UseGCLogFileRotation
  • -XX:+printgcdatestamps
  • The first parameter, PrintGCDetails, is disabled by default, but you can print a detailed status message after every GC event.
  • Numberofgclogfiles defines the maximum number of log files written for a single application. You can set it as per the demand by your application.
  • Gclogfilesize denotes the maximum size of every log file.
  • XLoggc option is used to set the file name and the location where the GC log output will be redirected and recorded. The actual file location will replace the <file path>.
  • UseGCLogFileRoation enables the log rotations. It is usually applied when long-running Java Applications produce huge log files. This parameter must be used with the -Xloggc:<filename> option to redirect the log output after every rotation.
  • Lastly,-XX:+printgctimestamps and -XX:+printgcdatestamps print the date-wise timestamp after every GC event in the log file.

Resolving the ‘OutofMemory’ error

A Java application is very much likely to run out of memory. It can happen for various reasons, such as errors in the code or memory leaks in the application. Some JVM options can be used to find the cause of the error by dumping the heap into a physical file for analysis.

You can use the following command to trigger the heap dump when an out-of-memory error occurs:


This is among the essential Java options as it is used to handle the very dreaded out-of-memory error. It produces a stack dump when your JVM crashes with an out-of-memory error. No resources are required to use this JVM option unless the error has occurred.

After the crash, the heap dump is usually set up in the “current directory” of the JVM by default, but you can create heap dumps on specific directories of your choice by using the following JVM options:

  • -XX:heapdumppath= <file path to heap dump>
  • -XX:+usegcoverheadlimit
  • -XX:onoutofmemoryerror="< cmd args >;< cmd args >"

The heap dump file is significantly larger can even go up to gigabytes, so you should make sure that the target file system has enough space available and it allows sufficient capacity.

If you wish to restart the servers whenever an out-of-memory error is encountered, this can also be done using the third option with these parameters:

XX:onoutofmemoryerror="shutdown -r"

Trace class loading and unloading

Java also offers some JVM Options for tracing class loading and unloading. Below are the two JVM options used to print the logging information whenever a class loads into JVM or unloads from the JVM.

  • -XX:+traceclassloading
  • -XX:+traceclassunloading

These JVM options work as flags. They prove to be very useful for identifying any memory leakage linked to the class loaders. Developers can easily find out by increasing the size of garbage collected while class loading and unloading a memory leakage.

JVM option to select 32/64 bit package

Among all the Java options, there is a straightforward option used to set the 32/64 environment. the JVM by default chooses 32-bit environmental packages if both 32-bit and 64-bit packages are installed on your operating system, but if you want to set the environment to 64-bit manually, it can be done using the following parameter,

-d<OS bit>

Which application is using JVM options?

It is convenient to find out all the Java options currently being used in your application. You can quickly check if a specific Java option is being used in your application or not. You can use the following command to identify the Java process and the JVM options printed as process arguments for an application running on Linux.

Ps -ef | grep Java


We have discussed some essential Java options available in JVM. JVM keeps Java applications stable by managing and optimizing all the programs on various devices and operating systems. Java options play an important part in it as it provides customization options to users, but these options tend to improve the stability to a certain extent. This article is meant to help you configure JVM options for your application. You can explore more Java options that are used for various other purposes in Java.

See Also: How To Diagnose Java Virtual Machine Crashes?

new Java jobs



Full Stack Java Developer | Writer | Recruiter, bridging the gap between exceptional talent and opportunities, for some of the biggest Fortune 500 companies.

Candidate signup

Create a free profile and find your next great opportunity.


Employer signup

Sign up and find a perfect match for your team.


How it works

Xperti vets skilled professionals with its unique talent-matching process.


Join our community

Connect and engage with technology enthusiasts.