如何以编程方式获取GC类直方图

时间:2021-12-07 14:55:23

I would like to get the same result of the follow command programmatically:

我想以编程方式获得跟随命令的相同结果:

jcmd <pid> GC.class_histogram | grep 'sun.util'

 728:            27           2592  sun.util.calendar.Gregorian$Date
 800:            44           2112  sun.util.locale.LocaleObjectCache$CacheEntry
 816:            36           2016  sun.util.calendar.ZoneInfo
1169:            22            880  sun.util.locale.BaseLocale$Key

...

I already searched for many answers, but none of the found is clear and do what I want, is there any way to do that?

我已经搜索了很多答案,但是找不到任何答案,并且做了我想做的事,有没有办法做到这一点?

2 个解决方案

#1


2  

The follow code shows the same result:

以下代码显示相同的结果:

import javax.management.MBeanException;
import javax.management.ReflectionException;

import com.sun.management.DiagnosticCommandMBean;

import sun.management.ManagementFactoryHelper;

@SuppressWarnings("restriction")
public class ClassHistogram {

    public static void main(String[] args) throws MBeanException, ReflectionException {
        DiagnosticCommandMBean dcmd = ManagementFactoryHelper.getDiagnosticCommandMBean();

        String[] emptyStringArgs = {};
        Object[] dcmdArgs = { emptyStringArgs };
        String[] signature = { String[].class.getName() };
        System.out.println(dcmd.invoke("gcClassHistogram", dcmdArgs, signature));
    }

}

Reference from the available commands: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

可用命令的参考:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

#2


0  

The jcmd manpage covers this.

jcmd手册页涵盖了这一点。

To invoke diagnostic commands from a remote machine or with different identifiers, you can use the com.sun.management.DiagnosticCommandMBean interface. For more information about the DiagnosticCommandMBean interface, see the API documentation at http://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html

要从远程计算机或使用不同标识符调用诊断命令,可以使用com.sun.management.DiagnosticCommandMBean接口。有关DiagnosticCommandMBean接口的更多信息,请参阅http://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html上的API文档。

#1


2  

The follow code shows the same result:

以下代码显示相同的结果:

import javax.management.MBeanException;
import javax.management.ReflectionException;

import com.sun.management.DiagnosticCommandMBean;

import sun.management.ManagementFactoryHelper;

@SuppressWarnings("restriction")
public class ClassHistogram {

    public static void main(String[] args) throws MBeanException, ReflectionException {
        DiagnosticCommandMBean dcmd = ManagementFactoryHelper.getDiagnosticCommandMBean();

        String[] emptyStringArgs = {};
        Object[] dcmdArgs = { emptyStringArgs };
        String[] signature = { String[].class.getName() };
        System.out.println(dcmd.invoke("gcClassHistogram", dcmdArgs, signature));
    }

}

Reference from the available commands: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

可用命令的参考:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html

#2


0  

The jcmd manpage covers this.

jcmd手册页涵盖了这一点。

To invoke diagnostic commands from a remote machine or with different identifiers, you can use the com.sun.management.DiagnosticCommandMBean interface. For more information about the DiagnosticCommandMBean interface, see the API documentation at http://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html

要从远程计算机或使用不同标识符调用诊断命令,可以使用com.sun.management.DiagnosticCommandMBean接口。有关DiagnosticCommandMBean接口的更多信息,请参阅http://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html上的API文档。