Open In App

Frequently Used JVM Parameters

Improve
Improve
Like Article
Like
Save
Share
Report

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

Example:

Java




// 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
  System.gc(); 
 
}


Output:

 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:+UseGCLogFileRotation 
-XX:NumberOfGCLogFiles=< number of log files > 
-XX:GCLogFileSize=< file size >[ unit ]
-Xloggc:/path/to/gc.log

Example:

Java




// Java Program to illustrate Print GC
public class Application {
 
    private static Map<String, String> stringContainer = new HashMap<>();
 
    public static void main(String[] args) {
        System.out.println("Start!");
        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!
        System.gc();
 
        // Remove 2 M out of 3 M
        for (int i = 0; i < 2000000; i++) {
            String newString = stringWithPrefix + i;
            stringContainer.remove(newString);
        }
 
        System.out.println("MAP size: " + stringContainer.size());
        System.out.println("End");
    }
}


 

 



Last Updated : 27 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads