我希望以编程方式获得跟随命令的相同结果:
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
...
我已经搜索了很多答案,但找不到任何答案,并且做了我想做的事,有没有办法做到这一点?
答案 0 :(得分:2)
以下代码显示相同的结果:
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));
}
}
可用命令的参考:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html
答案 1 :(得分:0)
jcmd manpage涵盖了此内容。
要从远程计算机或使用不同的标识符调用诊断命令,可以使用com.sun.management.DiagnosticCommandMBean接口。有关DiagnosticCommandMBean接口的更多信息,请参阅http://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html
上的API文档