开发者

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

I would like to just find out the time the JVM la开发者_如何学Pythonst 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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜