Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the time the JVM last garbage collected?

I would like to just find out the time the JVM last garbage collected. Is this possible? How can i find out?

like image 499
JavaRocky Avatar asked Dec 07 '25 04:12

JavaRocky


2 Answers

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.

like image 53
meriton Avatar answered Dec 08 '25 16:12

meriton


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

like image 45
JenEriC Avatar answered Dec 08 '25 18:12

JenEriC