find which type of garbage collector is running

Post JSE 5 ergonomics is intended to automatically select the appropriate type of garbage collector for you (among other things).

I would like to know if there is any way to confirm/know the type of garbage collector and performance goals chosen/current set by the JVM ergonomics.


Solution 1:

java -XX:+PrintCommandLineFlags -version

will show you the default garbage collector. I have also found the following page useful which details the default garbage collector for various operating systems.

Solution 2:

(For Java <= 8)

This command print the GC type of a running JVM:

jmap -heap <pid> | grep GC

For modern computer (multiple cpus, big memory), JVM will detect it as server machine, and use Parallel GC by default, unless you specify which gc to use via JVM flags explicitly.

e.g

jmap -heap 26806 | grep GC

Output:

Parallel GC with 8 thread(s)


@Update - for Java 9+

(Thanks to @JakeRobb's comment.)

Since Java 9, there are 2 changes relevant to this question:

  • Need to use jhsdb to attach to a java process or launch a debugger.
    Refer: jhsdb
  • The default gc is changed to G1.

Command format:

jhsdb jmap --heap --pid <pid> | grep GC

e.g

jhsdb jmap --heap --pid 17573 | grep GC

Output:

Garbage-First (G1) GC with 8 thread(s)