JVM Arguments in Java

Rupam Yadav Jan 30, 2023
  1. Types of Java Options or JVM Arguments in Java
  2. Use JVM Arguments to Increase or Decrease the Heap Memory in Java
  3. Use JVM Arguments to Handle the Out of Memory Error in Java
  4. Use JVM Arguments for Garbage Collection in Java
JVM Arguments in Java

In Java, when we want to compile and run a program, we use the javac and java commands with the class or file name.

But if we want to run the Java program with some modification like increased memory, we use the Java Virtual Machine Arguments, also called JVM Options.

Types of Java Options or JVM Arguments in Java

There can be three types of Java Options or Java VM arguments:

  1. Standard Options - These are the available arguments and can be used with every implementation of the JVM. We can get the list of all standard arguments using the command java in the command line.

    One of the most commonly used standard arguments is the -version command that returns the version of the installed JVM on the local machine.

  2. Non-Standard Options - Unlike the standard options, these options might or might not be a part of all the JVM implementations. They can change according to the machine.

    We can change the values of these arguments. To get the list of non-standard arguments, we use the command java -X. Every non-standard command starts with -X.

  3. Advanced Options - As the name suggests, these are the advanced arguments only used when changing the specific part of our local JVM. They start with -XX.

We will talk about the non-standard and advanced options in the following sections.

Use JVM Arguments to Increase or Decrease the Heap Memory in Java

When we create a large program that needs a lot of memory than allocated to it, we need to modify the heap. We need to use the JVM arguments to do this.

We have two arguments to start the application with modified heap memory. -Xms sets the minimum heap size, while the -Xmx command sets the maximum.

We need to specify the size we want to allocate with the commands.

 -Xms<size>        set initial Java heap size
 -Xmx<size>        set maximum Java heap size

We use the command below to set the maximum heap size to 2 GB.

 java -Xmx2g Java-Class-Name

Use JVM Arguments to Handle the Out of Memory Error in Java

Whenever a large program needs more memory than allocated, the out-of-memory error occurs and terminates. If we want to examine the memory leaks, we must dump the heap in a file.

The file is in HPROF binary format that can be opened to be examined in a supported tool.

We use the following command to dump the heap in case of the out of memory error.

-XX:+HeapDumpOnOutOfMemoryError

We need to point out the path to the file and the process id of the current process. To do that, we use the below command.

-XX:HeapDumpPath= path-to-the-hprof-file-with-<pid>

Use JVM Arguments for Garbage Collection in Java

If we want to change the algorithm for the garbage collection in Java, we use the following commands.

-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+USeParNewGC
-XX:+UseG1GC

Here, we can see that there are four types of garbage collectors that we can use, and every collector has a different motive for usage.

Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Method