简体   繁体   中英

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?

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

Depending on what application server you are running, you might get the statistics you are looking for in the admin console. Garbage Collection stats are exposed via jmx and almost all major app server has a jmx console. Otherwise we could use Jconsole to bind to remote jvm and get the garbage collection stats. (Even jconsole will use Jmx)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM