当前位置:网站首页>使用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
边栏推荐
- Ubutnu20 installer centernet
- 一些接地气的话儿
- Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
- Write table of MySQL Foundation (create table)
- Gsi-ecm digital platform for engineering construction management
- The more you use the computer, the slower it will be? Recovery method of file accidental deletion
- 41. The first missing positive number
- CUDA, NVIDIA driver, cudnn download address and version correspondence
- MySQL基础合集
- C# 知识
猜你喜欢
Question brushing plan - depth first search DFS (I)
[SQL] string series 2: split a string into multiple lines according to specific characters
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Google 尝试在 Chrome 中使用 Rust
Express③(使用Express编写接口、跨域有关问题)
GSI-ECM工程建设管理数字化平台
Unity solves Z-fighting
缓存淘汰算法初步认识(LRU和LFU)
常用60类图表使用场景、制作工具推荐
wait、waitpid
随机推荐
setInterval、setTimeout、requestAnimationFrame
UKFslam
Thinking after learning to type
Mysql database common sense storage engine
Elastic box model
ros功能包内自定义消息引用失败
阿里云回应用户注册信息泄露事件
How to use PM2 management application? Come in and see
vulnhub DC:1渗透笔记
mmap、munmap
韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
Resolve the error - error identifier 'attr_ id‘ is not in camel case camelcase
Singleton mode
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
IOT 设计与开发
Valueerror: invalid literal for int() with base 10 conversion error related to data type
Unity Odin ProgressBar add value column
又一款数据分析神器:Polars 真的很强大
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行