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