83

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.

1
  • 4
    was curious about ergonomics, specifically for GC and wanted to see it in action - before creating recommendations for production; tried jvisualvm, no joy. Commented Feb 17, 2011 at 4:36

10 Answers 10

108
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.

5
  • 5
    I see no garbage collector related flags/information in the output of that command. I'm on Windows 7 with Java 1.8.0_45.
    – Zero3
    Commented May 15, 2015 at 2:58
  • @Zero3 well, what do you see? Commented Jun 29, 2015 at 12:35
  • this shows the command line args being used with the jvm, but does not show the garbage collector being used: [user@server ~]$ java -XX:+PrintCommandLineFlags -version -XX:InitialHeapSize=2018388928 -XX:MaxHeapSize=32210157568 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode) eric wang's answer worked for me
    – liltitus27
    Commented Jul 3, 2018 at 14:50
  • 4
    Wrong answer here. Does not provide a solution.
    – user2587965
    Commented Dec 3, 2018 at 16:11
  • 6
    @liltitus27 You should look for possible values: +UseParallelGC, +UseSerialGC, +UseConcMarkSweepGC, ..., see docs.oracle.com/javase/10/gctuning/… for me this answer must be accepted as valid. Commented Aug 14, 2019 at 0:50
36

(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)

3
  • 2
    This should be the accepted answer. For Java 9 and newer, use jhsdb jmap --heap --pid <pid> | grep GC.
    – JakeRobb
    Commented Feb 22, 2019 at 15:17
  • If someone getting error like me - Supported versions are <versionX> Target VM is <versionY> use the jdk installation directory to run the above command, e.g. - <JDK_HOME>/bin/jmap -heap <PID> | grep GC (ofcourse for <=Java8) Commented Oct 10, 2020 at 8:54
  • 1
    I got an error running this on JDK 11 for some reason. Instead, jcmd <pid> VM.flags reported the the GC info I was looking for.
    – Michael R
    Commented Feb 21, 2021 at 21:44
25
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class GCInformation {

    public static void main(String[] args) {
            try {
                    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();

                    for (GarbageCollectorMXBean gcMxBean : gcMxBeans) {
                            System.out.println(gcMxBean.getName());
                            System.out.println(gcMxBean.getObjectName());
                    }

            } catch (RuntimeException re) {
                    throw re;
            } catch (Exception exp) {
                    throw new RuntimeException(exp);
            }
    }
}

e.g. try following commands to know various GC Type

java -XX:+PrintCommandLineFlags  GCInformation
java -XX:+PrintCommandLineFlags -XX:+UseParallelGC GCInformation
java -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC -XX:+UseParNewGC GCInformation
java -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC -XX:-UseParNewGC GCInformation
1
  • Error: Could not find or load main class GCInformation Caused by: java.lang.ClassNotFoundException: GCInformation Commented Sep 14, 2022 at 10:52
10

Not a direct answer to your question, but I believe this is what you're looking for.

According to Java 6 documentation 1 and 2 (not just Java 5):

Reference 1 says:

On server-class machines running the server VM, the garbage collector (GC) has changed from the previous serial collector [...] to a parallel collector

Reference 2 says:

Starting with J2SE 5.0, when an application starts up, the launcher can attempt to detect whether the application is running on a "server-class" machine and, if so, use the Java HotSpot Server Virtual Machine (server VM) instead of the Java HotSpot Client Virtual Machine (client VM).

Also, reference 2 says:

Note: For Java SE 6, the definition of a server-class machine is one with at least 2 CPUs and at least 2GB of physical memory.

From this information, you can know that if the box is a server (according to 2) then it will be using the parallel GC. You can also infer that it will not change GC during runtime.

You can probably find the right answer for non-server machines if you dig further into the documentation.

2
  • 1
    exactly! but have you tried this on a virtual machine? any idea what might happen if I allocated 1.75 cores to an instance? Commented Mar 3, 2011 at 9:37
  • I haven't, but my third quote says "at least 2 CPUs" so I'm guessing it will use the serial GC. Regarding your comment "wanted to see it in action - before creating recommendations for production", the recommendation is to always set the GC manually. Commented Mar 3, 2011 at 11:24
7

You could use the following VM arguments with JDK 14,

-Xlog:gc -Xlog:gc*

The log will be:

[0.008s][info][gc,heap] Heap region size: 1M
[0.008s][info][gc,heap,coops] Heap address: 0x0000000700000000, size: 4096 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
[0.011s][info][gc           ] Using G1
[0.011s][info][gc,cds       ] Mark closed archive regions in map: [0x00000007bff00000, 0x00000007bff7aff8]
[0.011s][info][gc,cds       ] Mark open archive regions in map: [0x00000007bfe00000, 0x00000007bfe50ff8]
[0.027s][info][gc           ] Periodic GC disabled
2
-XX:+PrintGC
-XX:+PrintGCDetails

This will print what GC is used. In my case it prints:

[GC (Allocation Failure) [PSYoungGen: 348192K->32K(348672K)] 356792K->8632K(1048064K), 0.0111518 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]

Which means that the Parallel Garbage collector is being used for the young generation. "Allocation Failure" means that garbage collection started because there were not enough space in young generation heap part.

3
  • I feel like this answer is incomplete. What part of that printout indicates the Parallel collector? Also, how do you invoke that against a running JVM?
    – JakeRobb
    Commented Feb 22, 2019 at 15:16
  • PS stands for Parallel Scavenge. docs.oracle.com/javase/jp/8/docs/serviceabilityagent/sun/jvm/… . You can pass those parameters to a new Java process that you start, meaning that you cannot apply it to already running process.
    – Alex T
    Commented Feb 24, 2019 at 20:24
  • In that case, I think Eric Wang’s answer is better, as you invoke it against a running JVM, and because it doesn’t assume you can translate impossibly generic abbreviations like “PS”. 🙂
    – JakeRobb
    Commented Feb 24, 2019 at 20:45
1

Use the GarbageCollectorMXBeans to obtain MemoryPoolMXBeans.

3
  • 1
    Well, this doesn't really give you the GC that is in use. Interestingly though, the 'getVMOption' output for UseSerialGC, UseParallelOldGC, UseParallelGC, UseConcMarkSweepGC, all report 'false'. Commented Feb 21, 2011 at 6:56
  • Sorry, I should have tried it before posting :-(. I had thought I had used it in the past for this...
    – Brett Kail
    Commented Feb 22, 2011 at 23:27
  • This should give you the GCs that may run against the JVM, by watching the collectionCount attributes you can see if they are being used. Commented Jan 16, 2012 at 23:05
1

Here's some info about how to programmatically get GC info, but it looks like it may need the name of the GC beforehand. Troublesome.

Edit: try ManagementFactory.getGarbageCollectorMXBeans() and iterate through the returned list. One of these will be active.

0
0

To achieve performance goal you need to check with various GC algorithms.

Available Collector in Oracle JDK

-XX:+UseSerialGC

-XX:+UseParallelGC

-XX:+UseG1GC

-XX:+UseConcMarkSweepGC

-XX:UseZGC

Turning on GC Logging in Java 7 / 8

java -XX:+PrintGCDetails -XX:+PrintGCDateStamps - Xloggc: <file-path>

Turning on GC Logging in Java 9 and Up

java –Xlog:gc*:file=<file-path>:filecount=10,filesize=10M

Dynamically Turning on GC Logging in Java 9 and Up

jcmd <pid> VM.log what=gc output=<file-path>

Try GC Log analyzing tool for compare GC logs

GC Log Analyzer - http://www.gcloganalyzer.com

-4

You can use -XX flag for JRE to choose the garbage collector of your choice.

Tuning Garbage Collection with the 5.0 Java TM Virtual Machine

Additionally, you can use JConsole to monitor garbage collection.

2
  • 2
    I know that you can choose your garbage collector, however, what I'm interested in is finding out which collector was chosen by the JVM for me; who knows, maybe it might even change strategies on-the-fly based on usage! Commented Feb 18, 2011 at 3:28
  • @ryan-fernandes from Garbage Collector Ergonomics, on server-class machines running the server VM, the default GC is parallel collector, else serial collector.
    – Garbage
    Commented Feb 18, 2011 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.