Wednesday, July 6, 2011

HotSpot JVM garbage collection options cheat sheet

Article was updated, here you can find latest version.

In this article I have collected a list of options related to GC tuning in JVM. This is not a comprehensive list, I have only collected options which I use in practice (or at least understand why I may want to use them).

HotSpot GC collectors

HotSpot JVM may use one of 6 combinations of garbage collection algorithms listed below.
Young collector
Old collector
JVM option
Serial (DefNew)
Serial Mark-Sweep-Compact
Parallel scavenge (PSYoungGen)
Serial Mark-Sweep-Compact (PSOldGen)
Parallel scavenge (PSYoungGen)
Parallel Mark-Sweep-Compact (ParOldGen)
Serial (DefNew)
Concurrent Mark Sweep
Parallel (ParNew)
Concurrent Mark Sweep

GC logging options

JVM option
General options
-verbose:gc or -XX:+PrintGC
Print basic GC info
Print more elaborated GC info
Print timestamps for each GC event (seconds count from start of JVM)
Redirects GC output to file instead of console
Print detailed demography of young space after each collection
Print TLAB allocation statistics
Print pause summary after each stop-the-world pause
Print time for each concurrent phase of GC
Creates heap dump file after full GC
Creates heap dump file before full GC
Creates heap dump in out-of-memory condition
Specifies path to save heap dumps
CMS specific options
Print additional CMS statistics if n >= 1
Print CMS initiation details
Print additional info concerning free lists
Print additional info concerning free lists
Dump useful information about the state of the CMS old generation upon a promotion failure.
In a CMS dump enabled by option above, include more detailed information about the free chunks.
In a CMS dump enabled by option above, include more detailed information about the allocated objects.

JVM sizing options

JVM option
-Xms<size> -Xmx<size>
Initial and max size of heap space (young space + tenured space). Permanent space does not count to this size.
Initial and max size of young space.
Alternative way to specify young space size. Sets ration of young vs tenured space (e.g. -XX:NewRatio=2 means that young space will be 2 time smaller than tenuted space).
Sets size of single survivor space as a portion of Eden space size (e.g. -XX:NewSize=64m -XX:SurvivorRatio=6 means that each survivor space will be 8m and eden will be 48m).
Initial and max size of permanent space.
-Xss=<size> or
Sets size of stack area dedicated to each thread. Thread stacks do not count to heap size.
Maximum size of off-heap memory available for JVM

Young collection tuning

JVM option
Initial value for tenuring threshold (number of collections before object will be promoted to tenured space).
Max value for tenuring threshold.
Max object size allowed to be allocated in young space (large objects will be allocated directly in old space). Thread local allocation bypasses this check so if TLAB is large enough object exciding size threshold still may be allocated in young.
Promote all objects surviving young collection immediately to tenured space (equivalent of -XX:MaxTenuringThreshold=0)
Objects from young space will never get promoted to tenured space while survivor space is large enough to keep them.
Thread local allocation blocks
Use thread local allocation blocks in young space. Enabled by default.
Allow JVM to adaptively resize TLAB for threads.
Initial size of TLAB for thread
Minimal allowed size of TLAB

CMS tuning options

JVM option
Controlling initial mark phase
Only use occupancy as a criterion for starting a CMS collection.
Percentage CMS generation occupancy to start a CMS collection cycle. A negative value means that CMSTriggerRatio is used.
Percentage CMS generation occupancy at which to initiate CMS collection for bootstrapping collection stats.
Percentage of MinHeapFreeRatio in CMS generation that is allocated before a CMS collection cycle commences.
Percentage of MinHeapFreeRatio in the CMS perm generation that is allocated before a CMS collection cycle commences, that also collects the perm generation.
Once CMS collection is triggered, it will wait for next young collection to perform initial mark right after. This parameter specifies how long CMS can wait for young collection.
Controlling remark phase
Force young collection before remark phase.
If Eden used is below this value, don't try to schedule remark
The Eden occupancy % at which to try and schedule remark pause
Start sampling Eden top at least before young generation occupancy reaches 1/n of the size at which we plan to schedule remark
Parallel execution
Use parallel algorithm for young space collection.
Use multiple threads for concurrent phases.
Number of parallel threads used for concurrent phase.
Number of parallel threads used for stop-the-world phases.
CMS incremental mode
Enable incremental CMS mode. Incremental mode is meant for severs with small number of CPU.
Miscellaneous options
If not enabled, CMS will not clean permanent space. You should always enable it in multiple class loader environments such as JEE or OSGi.
Let System.gc() trigger concurrent collection instead of full GC.
Same as above but also triggers permanent space collection.

Miscellaneous GC options

JVM option
JVM will ignore application calls to System.gc()


  1. Parallel (ParNew) Concurrent Mark Sweep
    Should be:

  2. Good collection man. GC tuning is a hard thing to achieve and require patience and profiling but indeed knowledge of these JVM options helps to understand the behavior.

    What is garbage collection in Java

  3. Hi

    I have a question. What jvm parameter I have to use to force the full garbage collection?


  4. I would suggest you add -XX:+PrintGCDateStamps which will prefix the wall clock time which is very helpfull to correlate GC events with Business incidents.

    BTW: some blogs report one can add the Date and remove the seconds, but in my tests -PrintGCTimeStamps seems not to help. (it is possible to remove them in the HotSpotsDiagnostics MBean).

    1. Actually -PrintGCTimeStamp works, if you speficy is after Xloggc. --eckes

  5. Missing -XX:+PrintReferenceGC