Files
javademos/g1-container/src/GcReport.java
T

32 lines
1.5 KiB
Java

import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.stream.Collectors;
/**
* Prints which garbage collector the JVM picked when nobody asked for one, and why.
*
* <p>The "why" is the interesting part: HotSpot records the ORIGIN of every flag value.
* {@code ERGONOMIC} means the JVM chose it by looking at the machine; {@code DEFAULT} means
* it is simply the built-in value; {@code COMMAND_LINE} means somebody typed it.
*
* <p>Explained in docs/02-g1-default.md. Run by scripts/g1-default.sh.
*/
public class GcReport {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
System.out.println("java.version = " + System.getProperty("java.version"));
System.out.println("availableCpus = " + rt.availableProcessors());
System.out.println("maxHeapMB = " + rt.maxMemory() / (1024 * 1024));
System.out.println("collector beans = " + ManagementFactory.getGarbageCollectorMXBeans().stream()
.map(GarbageCollectorMXBean::getName).collect(Collectors.joining(", ")));
HotSpotDiagnosticMXBean hs = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
for (String flag : new String[] {"UseG1GC", "UseSerialGC", "UseParallelGC"}) {
var opt = hs.getVMOption(flag);
System.out.printf("%-17s = %-5s (origin %s)%n", flag, opt.getValue(), opt.getOrigin());
}
}
}