I would like to just find out the time the JVM last garbage collected. Is this possible? How can i find out?
This ought to get you started:
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
System.out.println(gcBean.getCollectionCount());
com.sun.management.GarbageCollectorMXBean sunGcBean = (com.sun.management.GarbageCollectorMXBean) gcBean;
System.out.println(sunGcBean.getLastGcInfo().getStartTime());
}
The cast to sunGcBean is probably not portable to a non-sun JVM. You can find a vendor specific extension of your target VM, or watch gcBean.getCollectionCount() increasing in a watchdog thread that records the current time when it sees a change.
The simplest way is to start you Java application with -verbose:gc, which will dump information about garbage collection to stdout:
java -verbose:gc MyProgram
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With