从JVM内存获取所有对象实例的方法

huangapple go评论75阅读模式
英文:

A way to get all objects instances from JVM memory

问题

有没有一种方法可以从JVM内存中获取所有对象实例并将它们的toString()表示打印到文件中?

我需要在出现错误时获取我应用程序的所有对象实例的快照。

我认为转储(dump)不是我正在寻找的,因为它不会提供关于在某一时刻内存中包含了哪些实例的精确信息,只提供统计信息。

英文:

Is there a way to get all objects instances from JVM memory and print its' toString() representation in file?

I need to do a shot of all my app's objects instances in moment of error.

I think dump is not what I looking for because it doesn't give me precise information about what instances were contained in memory at the moment, only statictics information.

答案1

得分: 1

你所要求的实际上被称为堆转储。

堆转储是在给定时间内JVM内存的快照,它包含有关所有对象的信息。

要捕获它,您可以执行

jmap -dump:format=b,file=heap.bin <pid>
  • pid:Java进程的标识符

有几个工具可以分析输出的文件。很可能您的IDE可以做到这一点,这样您可以在熟悉的界面中查看它。

了解更多信息
https://dzone.com/articles/java-heap-dump-analyzer-1

要以编程方式触发堆转储,您可以像这样做

public class HeapDumper {
    
    private static final String HOTSPOT_BEAN_NAME =
        "com.sun.management:type=HotSpotDiagnostic";
    private static volatile HotSpotDiagnosticMXBean hotspotMBean;

    static void dumpHeap(String fileName, boolean live) {
        initHotspotMBean();
        try {
            hotspotMBean.dumpHeap(fileName, live);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

    private static void initHotspotMBean() {
        if (hotspotMBean == null) {
            synchronized (HeapDumper.class) {
                if (hotspotMBean == null) {
                    hotspotMBean = getHotspotMBean();
                }
            }
        }
    }
    
    private static HotSpotDiagnosticMXBean getHotspotMBean() {
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            HotSpotDiagnosticMXBean bean =
                ManagementFactory.newPlatformMXBeanProxy(server,
                                                         HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
            return bean;
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }
}

像这样触发它


String fileName = "heap.bin";
boolean live = true; // 仅可达对象 - true,所有对象 - false
dumpHeap(fileName, live);

英文:

What you are asking for is essentially called heap dump.

Heapdump is a snapshot of JVM memory in a given time, it contains info about all the objects.

To capture it you can do

jmap -dump:format=b,file=heap.bin &lt;pid&gt;
  • pid: id of the Java process

There are several tools that can analyze the file outputted. Good change is that your IDE can do it, that way you can view it in familiar interface.

More here
https://dzone.com/articles/java-heap-dump-analyzer-1

To programatically trigger hepdump you can do something like this

public class HeapDumper {
    
    private static final String HOTSPOT_BEAN_NAME =
        &quot;com.sun.management:type=HotSpotDiagnostic&quot;;
    private static volatile HotSpotDiagnosticMXBean hotspotMBean;

    static void dumpHeap(String fileName, boolean live) {
        initHotspotMBean();
        try {
            hotspotMBean.dumpHeap(fileName, live);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

    private static void initHotspotMBean() {
        if (hotspotMBean == null) {
            synchronized (HeapDumper.class) {
                if (hotspotMBean == null) {
                    hotspotMBean = getHotspotMBean();
                }
            }
        }
    }
    
    private static HotSpotDiagnosticMXBean getHotspotMBean() {
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            HotSpotDiagnosticMXBean bean =
                ManagementFactory.newPlatformMXBeanProxy(server,
                                                         HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
            return bean;
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }
}

Triggering it like this


String fileName = &quot;heap.bin&quot;;
boolean live = true; // only reachable object - true, all objects - false
dumpHeap(fileName, live);

huangapple
  • 本文由 发表于 2020年8月10日 21:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63341091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定