Frequently Used JVM Parameters
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:
Java
public class GFG {
public void finalize(){System.out.println( "Test example garbage collection" );}
public static void main(String args[]){
GFG object1 = new GFG();
GFG object2 = new GFG();
object1 = null ;
object2 = null ;
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
public class Application {
private static Map<String, String> stringContainer = new HashMap<>();
public static void main(String[] args) {
System.out.println( "Start!" );
String stringWithPrefix = "Prefix" ;
for ( int i = 0 ; i < 3000000 ; i++) {
String newString = stringWithPrefix + i;
stringContainer.put(newString, newString);
}
System.out.println( "MAP size: " + stringContainer.size());
System.gc();
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
Share your thoughts in the comments
Please Login to comment...