当前位置:网站首页>使用mbean 自动执行heap dump
使用mbean 自动执行heap dump
2022-04-23 20:50:00 【weixin_40455124】
记录下如何查看mbean的使用方法和自动进行heap dump。
import java.io.File;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanException;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
public class HeapDump {
//https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/DiagnosticCommandMBean.html
public static final String DIAGNOSTIC_COMMAND = "com.sun.management:type=DiagnosticCommand";
private static final String HEAP_COMMAND = "gcClassHistogram";
//can be useful for dumping heaps in binary format
//https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/HotSpotDiagnosticMXBean.html
public static final String HOTSPOT_DIAGNOSTIC = "com.sun.management:type=HotSpotDiagnostic";
private static final String DUMP_COMMAND = "dumpHeap";
private static final String DUMP_FILE_NAME = "heap";
private static final String DUMP_FILE_ENDING = ".hprof";
private static final boolean DUMP_DEAD_OBJECTS = false;
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
public static void dumpHeap(String dumpFolder)
throws MalformedObjectNameException, InstanceNotFoundException, MBeanException, ReflectionException {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName hotspotBean = ObjectName.getInstance(HOTSPOT_DIAGNOSTIC);
String timeSuffix = '-' + dateFormat.format(new Date());
File dumpFile = new File(dumpFolder, DUMP_FILE_NAME + timeSuffix + DUMP_FILE_ENDING);
//it needs to be with a system dependent path seperator
mBeanServer.invoke(hotspotBean, DUMP_COMMAND
, new Object[]{
dumpFile.getAbsolutePath(), DUMP_DEAD_OBJECTS}
, new String[]{
String.class.getName(), Boolean.TYPE.getName()});
}
/** * 打印mbean 使用方式 * HeapDump.getMBeanInfo(HeapDump.DIAGNOSTIC_COMMAND); * HeapDump.getMBeanInfo(HeapDump.HOTSPOT_DIAGNOSTIC); * @param mbeanName * @throws MalformedObjectNameException * @throws NullPointerException * @throws IntrospectionException * @throws InstanceNotFoundException * @throws ReflectionException */
public static void getMBeanInfo(String mbeanName) throws MalformedObjectNameException, NullPointerException, IntrospectionException, InstanceNotFoundException, ReflectionException {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName hotspotBean = ObjectName.getInstance(mbeanName);
javax.management.MBeanInfo mbeanInfo = server.getMBeanInfo(hotspotBean);
MBeanOperationInfo[] operations = mbeanInfo.getOperations();
for (int i=0;i<operations.length;i++) {
System.out.println(operations[i].toString());
}
}
public static void getHeap()
throws MalformedObjectNameException, InstanceNotFoundException, MBeanException, ReflectionException {
ObjectName diagnosticObjectName = ObjectName.getInstance(DIAGNOSTIC_COMMAND);
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
String reply = (String) mBeanServer.invoke(diagnosticObjectName, HEAP_COMMAND
, new Object[]{
new String[]{
}}, new String[]{
String[].class.getName()});
System.out.println(reply);
}
}
以上代码核心来源:
https://gist.githubusercontent.com/munendrasn/2b67831b7a62d4db90c12467466a9736/raw/0679a2bb54f56a416b71912b8890a2473b336a75/HeapDump.java
版权声明
本文为[weixin_40455124]所创,转载请带上原文链接,感谢
https://radishgz.blog.csdn.net/article/details/124366174
边栏推荐
- Preliminary understanding of cache elimination algorithm (LRU and LFU)
- go slice
- pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
- Is qiniu school useful and is the recommended securities account safe
- ROS学习笔记-----ROS的使用教程
- Zhongchuang storage | how to choose a useful distributed storage cloud disk
- Flex layout
- Question brushing plan - depth first search DFS (I)
- Common problems in deploying projects with laravel and composer for PHP
- Tensorflow realizes gradient accumulation, and then returns
猜你喜欢

Addition, deletion, modification and query of MySQL advanced table

C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序

Problem brushing plan -- dynamic programming (III)

Express③(使用Express编写接口、跨域有关问题)

Graph traversal - BFS, DFS

Keywords static, extern + global and local variables

Zhongchuang storage | how to choose a useful distributed storage cloud disk

MySQL basic collection

go slice

居家第二十三天的午饭
随机推荐
Matlab: psychtoolbox installation
ubutnu20安裝CenterNet
Rust更适合经验较少的程序员?
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Awk print special characters
How to use PM2 management application? Come in and see
MySQL basic collection
Async function ------ ES6
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
Preliminary understanding of cache elimination algorithm (LRU and LFU)
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
Vulnhub DC: 1 penetration notes
MySQL数据库常识之储存引擎
PHP的Laravel与Composer部署项目时常见问题
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
Another data analysis artifact: Polaris is really powerful
Awk example skills
[matlab 2016 use mex command to find editor visual studio 2019]
学会打字后的思考