gpt4 book ai didi

jmx - 使用 jmx 从命令行获取堆转储

转载 作者:行者123 更新时间:2023-12-04 02:13:54 28 4
gpt4 key购买 nike

是否可以从命令行使用 JMX 在 linux (CentOS) 上运行的进程上获取服务器堆转储?

无法打开 VisualVM,无法安装jmap

最佳答案

可以用这个简单的代码来完成:

import com.sun.management.HotSpotDiagnosticMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

@SuppressWarnings("restriction")
public class CreateHeapDump
{
public static void main(String[] args) throws Exception
{
String host = args[0];
String port = args[1];

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
HotSpotDiagnosticMXBean bean = JMX.newMBeanProxy(mbsc, mbeanName, HotSpotDiagnosticMXBean.class, true);

String fileName = "heap_dump_" + new SimpleDateFormat("dd.MM.yyyy HH.mm").format(new Date()) + ".hprof";
boolean onlyLiveObjects = true;
bean.dumpHeap(fileName, onlyLiveObjects);
}
}

编译它:

javac CreateHeapDump.java

从命令行调用它:

java CreateHeapDump localhost 9010

关于jmx - 使用 jmx 从命令行获取堆转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725960/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com