Most important VM (JVM) PARAMETERS with examples in JVM Heap memory in java | Command Line arguments




In this core java tutorial we will learn What are Most important and frequently used VM (JVM) PARAMETERS in JVM Heap memory in java.


Contents of page >
A) VM (JVM) PARAMETERS in JVM Heap memory >
-Xms
-Xmx  
  1. Young Generation(VM PARAMETERS for Young Generation)
-Xmn
-XX:NewRatio
-XX:NewSize
-XX:MaxNewSize
-XX:SurvivorRatio :   (for survivor space)
  1. Old Generation (tenured) - (VM PARAMETERS for Old Generation)
          -XX:NewRatio :NewRatio controls the size of young and old generation.
  1. Permanent Generation (VM PARAMETERS for Permanent Generation)
-XX:PermSize
-XX:MaxPermSize
B) Other important VM (JVM) parameters for java heap in java >
-Xss
-XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio
-XX:+AggressiveHeap


C) Vm (JVM) option for enabling serial GC (garbage Collector) in java >
-XX:+UseSerialGC


D.0) Vm (JVM) option for enabling throughput GC (Garbage collector) in java >
-XX:+UseParallelGC


D.1) Vm (JVM) option for enabling throughput collector with n number of threads in java >
-XX:ParallelGCThreads=<numberOfThreads>


E.0). Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC


E.1) Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector with n number of threads in java >
-XX:ParallelCMSThreads=<n>


F). Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
-XX:+UseG1GC



Before learning about Most important VM (JVM) PARAMETERS with examples in JVM Heap memory in java we must know about JVM Heap memory (Hotspot heap structure).

JVM Heap memory (Hotspot heap structure) with diagram in java >

JVM Heap memory (Hotspot heap structure)  in java consists of following elements>
  1. Young Generation
    • 1a) Eden,
    • 1b) S0 (Survivor space 0)
    • 1c) S1 (Survivor space 1)
  2. Old Generation (Tenured)
  3. Permanent Generation.


A) VM (JVM) PARAMETERS in JVM Heap memory >


Learn how to pass vmargs (VM parameters) to java program in eclipse?

-Xms : Xms is minimum heap size which is allocated at initialization of JVM.


Examples of using -Xms VM (JVM) option in java >
Example1 of using -Xms VM (JVM) option in java >
java -Xms512 MyJavaProgram
It will set the minimum heap size of JVM to 512 bytes.


Example2 of using -Xms VM (JVM) option in java >
java -Xms512k MyJavaProgram
It will set the minimum heap size of JVM to 512 kilobytes.


Example3 of using -Xms VM (JVM) option in java >
java -Xms512m MyJavaProgram
It will set the minimum heap size of JVM to 512 megabytes.


Example4 of using -Xms VM (JVM) option in java >
java -Xms1g MyJavaProgram
It will set the minimum heap size of JVM to 1 gigabyte.


-Xmx : Xmx is the maximum heap size that JVM can use.


Examples of using -Xmx VM option in java >
Example1 of using -Xmx VM (JVM) option in java >
java -Xmx512 MyJavaProgram
It will set the maximum heap size of JVM to 512 bytes.


Example2 of using -Xmx VM (JVM) option in java >
java -Xmx512k MyJavaProgram
It will set the maximum heap size of JVM to 512 kilobytes.


Example3 of using -Xmx VM (JVM) option in java >
java -Xmx512m MyJavaProgram
It will set the maximum heap size of JVM to 512 megabytes.


Example4 of using -Xmx VM (JVM) option in java >
java -Xmx1g MyJavaProgram
It will set the maximum heap size of JVM to 1 gigabyte.

For more explanation and example - Read : What are -Xms and -Xmx JVM parameters and differences between them



  1. Young Generation(VM PARAMETERS for Young Generation)


-Xmn : -Xmn sets the size of young generation.
Examples of using -Xmn VM (JVM) option in java >
Example1 of using -Xmn VM (JVM) option in java >
java -Xmn512 MyJavaProgram
It will set the size of young generation of JVM heap to 512 bytes.


Example2 of using -Xmn VM (JVM) option in java >
java -Xmn512k MyJavaProgram
It will set the size of young generation of JVM heap to 512 kilobytes.


Example3 of using -Xmn VM (JVM) option in java >
java -Xmn512m MyJavaProgram
It will set the size of young generation of JVM heap to 512 megabytes.


Example4 of using -Xmn VM (JVM) option in java >
java -Xmn1g MyJavaProgram
It will set the size of young generation of JVM heap to 1 gigabyte.


For more explanation and example - Read :  -Xmn JVM parameters


-XX:NewRatio : NewRatio controls the size of young generation.
Example1 of using -XX:NewRatio  VM option in java >
-XX:NewRatio=3 means that the ratio between the young and old/tenured generation is 1:3.
In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.


Example2 of using -XX:NewRatio  VM option in java >
-XX:NewRatio=2 means that the ratio between the young and old/tenured generation is 1:2.
In other words, the combined size of the eden and survivor spaces will be one third of the total heap size.


For more explanation and example - Read :  What is -XX:NewRatio JVM parameters in java

-XX:NewSize - NewSize is minimum size of young generation which is allocated at initialization of JVM.
Note : If you have specified -XX:NewRatio than minimum size of the young generation is allocated automatically at initialization of JVM.


-XX:MaxNewSize - MaxNewSize is the maximum size of young generation that JVM can use.

    • 1a) Eden,
    • 1b) S0 (Survivor space 0)
    • 1c) S1 (Survivor space 1)
-XX:SurvivorRatio :   (for survivor space)
SurvivorRatio can be used to tune the size of the survivor spaces, but this is often not as important for performance.
Example of using -XX:SurvivorRatio > -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6.
In other words, each survivor space will be one eighth of the young generation (not one seventh, because there are two survivor spaces).


What if survivor spaces are too small?
If survivor spaces are too small, copying collection overflows directly into the tenured generation.


What if survivor spaces are too large?
If survivor spaces are too large, they will be uselessly empty. At each garbage collection the virtual machine chooses a threshold number of times an object can be copied before it is tenured. This threshold is chosen to keep the survivors half full. -XX:+PrintTenuringDistribution can be used to show this threshold and the ages of objects in the new generation. It is also useful for observing the lifetime distribution of an application.


  1. Old Generation (tenured) - (VM PARAMETERS for Old Generation)
-XX:NewRatio : NewRatio controls the size of young and old generation.
Example of using -XX:NewRatio, -XX:NewRatio=3 means that the ratio between the young and old/tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.


  1. Permanent Generation (VM PARAMETERS for Permanent Generation)


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.


Examples of using -XX:PermSize VM (JVM) option in java >
Example1 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=512 MyJavaProgram
It will set initial value of Permanent Space as 512 bytes to JVM.


Example2 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=512k MyJavaProgram
It will set initial value of Permanent Space as 512 kilobytes to JVM


Example3 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=512m MyJavaProgram
It will set initial value of Permanent Space as 512 megabytes to JVM


Example4 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=1g MyJavaProgram
It will set initial value of Permanent Space as 512 gigabyte to JVM

-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


Examples of using -XX:MaxPermSize VM option in java >
Example1 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=512 MyJavaProgram
It will set maximum value of Permanent Space as 512 bytes to JVM.


Example2 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=512k MyJavaProgram
It will set maximum value of Permanent Space as 512 kilobytes to JVM


Example3 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=512m MyJavaProgram
It will set maximum value of Permanent Space as 512 megabytes to JVM


Example4 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=1g MyJavaProgram
It will set maximum value of Permanent Space as 1 gigabyte to JVM

What is the maximum perm gen size value you can set on your system, if you aren’t sure about the system configurations?
Try out for -XX:MaxPermSize=256m, if it works then try -XX:MaxPermSize=512m, if it works then try -XX:MaxPermSize=1024m and so on. Be cautious before going beyond 8g.


B) Other important VM (JVM) parameters for java heap in java >


-Xss > Use this VM option to adjust the maximum thread stack size.


Also you must know that -Xss option is same as -XX:ThreadStackSize.


Examples of using -Xss VM option in java >
Example1 of using -Xss >
java -Xss512 MyJavaProgram
It will set the default stack size of JVM to 512 bytes.


Example2 of using -Xss >
java -Xss512k MyJavaProgram
It will set the default stack size of JVM  to 512 kilobytes.


Example3 of using -Xss >
java -Xss512m MyJavaProgram
It will set the default stack size of JVM  to 512 megabytes.


Example4 of using -Xss >
java -Xss1g MyJavaProgram
It will set the default stack size of JVM  to 1 gigabyte.

-XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio
JVM can grows or shrinks the heap to keep the proportion of free space to live objects within a specific range.
Note : this range is always specified in percentage.


-XX:MinHeapFreeRatio   > MinHeapFreeRatio is the minimum size that JVM can shrink the heap to keep the proportion of free space to live objects.
Default value of -XX:MinHeapFreeRatio is 40%
Example of setting -XX:MinHeapFreeRatio >  -XX:MaxHeapFreeRatio=40


-XX:MaxHeapFreeRatio > MaxHeapFreeRatio is the maximum size that JVM can grow the heap to keep the proportion of free space to live objects.
Default value of -XX:MaxHeapFreeRatio is 70%
Example of setting -XX:MaxHeapFreeRatio >  -XX:MaxHeapFreeRatio=70


-XX:+AggressiveHeap is used for Garbage Collection Tuning setting.
This VM option inspects the server resources and attempts to set various parameters in optimal manner for for long running and memory consuming applications. There must be minimum of 256MB of physical memory on the servers before the AggressiveHeap can be used.

C) Vm (JVM) option for enabling serial GC (garbage Collector) in java >
-XX:+UseSerialGC
Example of Passing Serial GC in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseSerialGC -jar d:\MyJar.jar



D.0) Vm (JVM) option for enabling throughput GC (Garbage collector) in java >
-XX:+UseParallelGC


Example of using throughput collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseParallelGC -jar d:\MyJar.jar

D.1) Vm (JVM) option for enabling throughput collector with n number of threads in java >
-XX:ParallelGCThreads=<numberOfThreads>


With this Vm (JVM) option you get a
  • Multi-threaded young generation garbage collector in java,
  • single-threaded old generation garbage collector in java and
  • single-threaded compaction of old generation in java.


Example of using throughput collector with n number of threads in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseParallelGC -XX:ParallelGCThreads=5 -jar d:\MyJar.jar
Here, 5 will be the number of threads which will be used for garbage collection by Throughput Collector.


D.2) Another Vm (JVM) option for enabling throughput collector in java >
-XX:+UseParallelOldGC


Note : Young generation collector is a copy collector so there is no need for compaction there in java.


Example of using throughput collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseParallelOldGC -jar d:\MyJar.jar



E.0). Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC


Example of using Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -jar d:\MyJar.jar


E.1) Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector with n number of threads in java >
-XX:ParallelCMSThreads=<n>


Example of using Concurrent Mark Sweep (CMS) collector with n number of threads in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=5 -jar d:\MyJar.jar
Here, 5 will be the number of threads which will be used for garbage collection by Concurrent Mark Sweep (CMS) Collector.



F). Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
-XX:+UseG1GC


Example of using G1 Garbage Collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseG1GC -jar d:\MyJar.jar


You must not use -XX:+UseParallelGC with -XX:+UseConcMarkSweepGC.




Summary -


I am highlighting all the summary portion to keep it separate from whole tutorial. Please refer above for complete detail.


A) VM (JVM) PARAMETERS in JVM Heap memory >
-Xms : Xms is minimum heap size which is allocated at initialization of JVM.


-Xmx : Xmx is the maximum heap size that JVM can use.

  1. Young Generation (VM PARAMETERS for Young Generation) -


-Xmn : -Xmn sets the size of young generation.


-XX:NewRatio : NewRatio controls the size of young generation.

-XX:NewSize - NewSize is minimum size of young generation which is allocated at initialization of JVM.


-XX:MaxNewSize - MaxNewSize is the maximum size of young generation that JVM can use.


-XX:SurvivorRatio :  (for survivor space)
SurvivorRatio can be used to tune the size of the survivor spaces.


  1. Old Generation (tenured) (VM PARAMETERS for Old Generation)
-XX:NewRatio : NewRatio controls the size of young and old generation.


  1. Permanent Generation (VM PARAMETERS for Permanent Generation) .


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.


-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.


-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


B) Other important VM (JVM) parameters for java heap in java >


-Xss > Use this VM option to adjust the maximum thread stack size.


-XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio
JVM can grows or shrinks the heap to keep the proportion of free space to live objects within a specific range.


-XX:+AggressiveHeap is used for Garbage Collection Tuning setting.

C) Vm (JVM) option for enabling serial GC (garbage Collector) in java >
-XX:+UseSerialGC
Example of Passing Serial GC in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseSerialGC -jar d:\MyJar.jar

D.0) Vm (JVM) option for enabling throughput GC (Garbage collector) in java >
-XX:+UseParallelGC


Example of using throughput collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseParallelGC -jar d:\MyJar.jar


D.1) Vm (JVM) option for enabling throughput collector with n number of threads in java >
-XX:ParallelGCThreads=<numberOfThreads>


E.0). Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java >
-XX:+UseConcMarkSweepGC


Example of using Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -jar d:\MyJar.jar

F). Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java >
-XX:+UseG1GC


Example of using G1 Garbage Collector in Command Line for starting jar>
java -Xms256m -Xms512m  -XX:+UseG1GC -jar d:\MyJar.jar


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>








Important VM parameters >

>What are -Xms and -Xmx JVM parameters in java, And differences between them with examples

>-Xmn JVM parameters in java with examples - Setting young generation size

>What is -XX:NewRatio JVM parameters in java with examples - Setting young and old generation ratio

>What are -XX:NewSize and -XX:MaxNewSize JVM parameters in java

>-XX:SurvivorRatio JVM parameters in java with examples - Setting survivor spaces size

>-XX:+AggressiveHeap VM parameters

>What are -XX:PermSize and -XX:MaxPermSize JVM parameters with examples in java | Differences

>Solve java.lang.OutOfMemoryError : unable to create new native Thread - Xss JVM option

More VM parameters >

>How to use -verbose:gc VM argument

>-Xverify option in java


Apache tomcat server,outOfMemory and Garbage collection in java >

>How to set or change permgen size in tomcat server, eclipse?

>How to set, change, increase or decrease heap size in tomcat server and eclipse to avoid OutOfMemoryError ?

>How to pass VM argument to tomcat in eclipse


Pass VM para through CMD, eclipse to java program and to Apache tomcat >

>How to write java program to pass VM/JVM parameters through CMD

>How to pass vmArgs(JVM parameters) to java program in eclipse

>How to pass VM argument to tomcat in eclipse



eEdit
Must read for you :