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
(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)
(Thanks to @JakeRobb's comment.)
Since Java 9, there are 2 changes relevant to this question:
jhsdb
to attach to a java process or launch a debugger.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)
Use the GarbageCollectorMXBeans to obtain MemoryPoolMXBeans.
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
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.
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
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.