简体   繁体   English

JAVA打印堆转储

[英]JAVA Print Heap Dump

I work with Java 1.7 and I want to print Heap Dump from java 我使用Java 1.7,我想从java打印Heap Dump

...
Object heapDump=.... ;
...

System.out.println(heapDump);

Can anybody help me? 有谁能够帮助我?

Use jmap : 使用jmap

jmap -dump:format=b,file=heap.bin <pid>

refer to Java 7 jmap tutorial . 参考Java 7 jmap教程

您可以使用HotSpotDiagnosticMXBean以编程方式创建堆转储。

You can't print the Heap Dump directly using SOP Statement. 您无法使用SOP声明直接打印堆转储。 But you can dump all the data in a file. 但是您可以将所有数据转储到文件中。 JVM will create heap dump every time when your application throws an OutOfMemoryError . 每次应用程序抛出OutOfMemoryError时,JVM都会创建堆转储。 HeapDumpPath is used to set location of heap dumps. HeapDumpPath用于设置堆转储的位置。

We can also use jmap from our code. 我们也可以使用代码中的jmap Assume name,pid are the fileds retrieving. 假设name,pid是fileds检索。 To get a pid from code use we need to use java.lang.management.ManagementFactory . 要从代码使用中获取pid,我们需要使用java.lang.management.ManagementFactory

String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.substring(0, name.indexOf("@"));
After that we can start jmap process like this:
String[] cmd = { "jmap", "-dump:file=D:\\temp\\heapdumps\\dump.bin", pid };
Process p = Runtime.getRuntime().exec(cmd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM