Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find which type of garbage collector is running

Tags:

java

jvm

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.

like image 715
Ryan Fernandes Avatar asked Feb 17 '11 04:02

Ryan Fernandes


People also ask

How do you know which garbage collector to use?

You can use -XX flag for JRE to choose the garbage collector of your choice. Additionally, you can use JConsole to monitor garbage collection.

What does the garbage collector run?

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks.

Which garbage collector is used by default?

G1 Garbage Collector is the default garbage collection of Java 9. G1 collector replaced the CMS collector since it's more performance efficient. How G1 Garbage Collector works is different from other collectors. Unlike other collectors, the G1 collector partitions the heap space into multiple equal-sized regions.


8 Answers

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.

like image 138
Darren Avatar answered Oct 03 '22 21:10

Darren


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

like image 35
user218867 Avatar answered Oct 03 '22 20:10

user218867


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
like image 43
Ankit Dimania Avatar answered Oct 03 '22 21:10

Ankit Dimania


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.

like image 37
Lucas Zamboulis Avatar answered Oct 03 '22 20:10

Lucas Zamboulis


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

like image 38
Alex T Avatar answered Oct 03 '22 19:10

Alex T


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
like image 25
Woody Sun Avatar answered Oct 03 '22 20:10

Woody Sun


Use the GarbageCollectorMXBeans to obtain MemoryPoolMXBeans.

like image 27
Brett Kail Avatar answered Oct 03 '22 19:10

Brett Kail


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.

like image 24
Chris Dennett Avatar answered Oct 03 '22 20:10

Chris Dennett