Skip to content
Related Articles
Open in App
Not now

Related Articles

Frequently Used JVM Parameters

Improve Article
Save Article
  • Last Updated : 27 Apr, 2021
Improve Article
Save Article

A JVM or Java Virtual Machine is a software implementation of a physical machine, or we can say it is an abstract machine. Java was designed with the concept of WORA (Write Once Run Anywhere), which runs on a VM. It is a specification that gives a runtime environment during which java bytecode is often executed. The compiler compiles the Java file into a Java .class file, then that .class file is input into the JVM, which loads and executes the class file.

JVM is constructed with three basic subunits

  1. ClassLoader Subsystem
  2. Runtime Data Area
  3. Execution Engine

Now let’s discuss the most frequently used JVM Parameters which are 3 namely as follows:

  1. Java Heap Size
  2. Garbage Collector
  3. Print GC

Parameter 1: Java Heap Size

The following three JVM options specify initial and max heap size and thread stack size while running Java programs: 

 -Xms - set initial Java heap size
 -Xmx - set maximum Java heap size
 -Xss - set java thread stack size

Parameter 2: Garbage Collector

Garbage Collection algorithms are used to attain better stability of the application.  Garbage Collection tracks each and each object available within the JVM heap space and removes unused ones.

Java provides us with 4 ways to implement garbage collection namely below:

  1. -XX:+UseSerialGC
  2. -XX:+UseParallelGC
  3. -XX:+USeParNewGC
  4. -XX:+UseG1GC



// Class
// To test garbage collection
public class GFG { 
  // Method 1 - finalize()
  // finalize() method is invoked each time
  // before the "Test example garbage collection"
  public void finalize(){System.out.println("Test example garbage collection");} 
  // Method 2
  // Main driver method
  public static void main(String args[]){
  // Creating anonymous objects of
  // GFG class in amin() method 
  GFG object1 = new GFG(); 
  GFG object2 = new GFG();
  // Assigning objects NULL references
  object1 = null
  object2 = null;
  // CAlling(invoking) garbage collection
  // using gc() method


 Test example garbage collection
 Test example garbage collection

Parameter 3: Print GC

These JVM options enable rubbish collection logging, which is very effective for the latency-sensitive operation.

Using the following parameters, we can log the GC activity:

-XX:NumberOfGCLogFiles=< number of log files > 
-XX:GCLogFileSize=< file size >[ unit ]



// Java Program to illustrate Print GC
public class Application {
    private static Map<String, String> stringContainer = new HashMap<>();
    public static void main(String[] args) {
        String stringWithPrefix = "Prefix";
        // Load Java Heap with 3 M java.lang.String instances
        for (int i = 0; i < 3000000; i++) {
            String newString = stringWithPrefix + i;
            stringContainer.put(newString, newString);
        System.out.println("MAP size: " + stringContainer.size());
        // Explicit GC!
        // Remove 2 M out of 3 M
        for (int i = 0; i < 2000000; i++) {
            String newString = stringWithPrefix + i;
        System.out.println("MAP size: " + stringContainer.size());



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!