By Alvin Alexander. Last updated: June 4, 2016
Java/JVM FAQ: How can I see which garbage collector is running when I run a java
command?
Use the -XX:+PrintCommandLineFlags
command line argument, as shown here:
$ java -XX:+PrintCommandLineFlags -version -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+UseCompressedOops -XX:+UseParallelGC java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
You can see from that output that the java
command (the JVM) is using the parallel garbage collector (-XX:+UseParallelGC) by default. You can verify that this works by telling the JVM to use a different garbage collector and then running the same command, like this:
$ java -XX:+UseG1GC -XX:+PrintCommandLineFlags -version -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+UseCompressedOops -XX:+UseG1GC java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
In that command I first specify a different garbage collector with -XX:+UseG1GC
, and then run the command, and as you can see, the output reflects the new GC.