Is it possible to create a Java dump when a special Exception happens? As I need to check the dump memory when the exception happens, For example when the nullpointerexception happens, it can geneate the dump automatically. We know .net platform has such a tool procdump https://technet.microsoft.com/en-us/sysinternals/dd996900.aspx, I have no idea if Java has a similar way?
Asked
Active
Viewed 350 times
1
-
1Try to check this: http://five.agency/java-heap-dump/ and http://docs.oracle.com/javase/7/docs/technotes/tools/share/jhat.html – user1097772 Jan 10 '17 at 12:25
-
I would use a debugger and breakpoint when this exception is thrown. – Peter Lawrey Jan 10 '17 at 12:46
-
@user1097772 , this just deal with the OOM exception, however we need to capture any type of exception. – YuFeng Shen Jan 11 '17 at 01:53
-
@Peter , which debugger you used, any details? Also it can handle any exception? – YuFeng Shen Jan 11 '17 at 01:54
-
In your debugger, you can add a break point to trigger when any specific Exception occurs. At this point you can see what your program and all it's threads are doing as well as look at all the data in your application. – Peter Lawrey Jan 11 '17 at 10:48
1 Answers
0
You can handle the required exception and put below code inside catch to capture heap-dump at any moment you want
ObjectName memoryMXBean = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
Object[] params = new Object[] { "myHeapDump.hprof", Boolean.TRUE };
String[] signature = new String[] { String.class.getName(), boolean.class.getName() };
Object result = connection.invoke(memoryMXBean, "dumpHeap", params, signature);

Henrik Aasted Sørensen
- 6,966
- 11
- 51
- 60

Anil Agrawal
- 2,748
- 1
- 24
- 31
-
It would need to write the code in all places which exception possible happens, and also needs to change the code, however I need to get the dump without rewriting the code like procdump. – YuFeng Shen Jan 11 '17 at 01:55
-
@Hermas You can go through http://stackoverflow.com/a/23640439/4090550 I did't find time to test the same, but you may try – Anil Agrawal Jan 11 '17 at 07:07