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
- ClassLoader Subsystem
- Runtime Data Area
- Execution Engine
Now let's discuss the most frequently used JVM Parameters which are 3 namely as follows:
- Java Heap Size
- Garbage Collector
- 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:
- -XX:+UseSerialGC
- -XX:+UseParallelGC
- -XX:+USeParNewGC
- -XX:+UseG1GC
Example:
// 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 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");
}
}
 Â