Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to instantiate a jvm from a heap dump?

Tags:

Everyone knows that a heap dump can be obtained from a running JVM. Is the other way possible? Can we start a JVM using a heap dump?

I have been having this question in mind for a long time now. If this is possible it would solve a lot of time and make thinks easy for a support engineer. It helps big time in cases where if we have to recreate some the rare problems our customer face. [Just imagine that the underlying hardware and Java runtime are the same and also all the supporting files are also present in the respective location in file system].

Added note: The intention of doing this is not when OOM occurs but at any given point after JVM starts.

like image 992
Siva Avatar asked Dec 03 '14 09:12

Siva


People also ask

What is JVM heap dump?

A heap dump is a snapshot of all the objects in the Java Virtual Machine (JVM) heap at a certain point in time. The JVM software allocates memory for objects from the heap for all class instances and arrays.

How do I get a heap dump from a running JVM?

Right-click on one of the Java process. Click on the 'Heap Dump' option on the drop-down menu. Heap dump will be generated. File path where heap dump is generated will be specified in the Summary Tab > Basic Info > File section.

Does taking heap dump trigger GC?

So, by triggering a heap dump, you are forcing a Full GC - regardless of whether the heap needs one or not. That is why the GC didn't occur automatically - it did not need to run, but you forced it to. It is very unlikely that there is a regression or other problem with JDK 10 running JDK 8 bytecode.

Is heap dump same as thread dump?

A thread dump is a dump of the stacks of all live threads. Thus useful for analysing what an app is up to at some point in time, and if done at intervals handy in diagnosing some kinds of 'execution' problems (e.g. thread deadlock). A heap dump is a dump of the state of the Java heap memory.


2 Answers

No you can't. You would need things like the current position in each open file. That affects what data is returned on a simple sequential read. The restorer would need to open each file and get it to the correct position. That may not be possible for non-seekable streams.

Program specific serialization is a much more feasible path, then setup the program from there.

Also, as Heap Dumps are usually from OutOfMemory situtaions, recreating JVM from same would again throw OutOfMemoryException. If you are taking heap dump in between, then serialize your objects and restore them when you bring up jvm.

(contents copied from comments of this question, Authors almas-shaikh and patricia-shanahan)

like image 93
3 revs Avatar answered Sep 20 '22 14:09

3 revs


To create a heap dump from a running JVM you can also use jhat, or jcmd (with the GC.heap_dump command), both exist in the JDK/bin folder. MAT is one way of analyzing the contents of the dump. Java Mission Control has a tool called JOverflow that analyzes heap dumps, but only to look at memory waste patterns.

I have never heard of any way to restart a JVM from sort of image, a heap dump would not be enough at all, since it only contains the Java objects, and not the compiled code and other things.

like image 29
Klara Avatar answered Sep 20 '22 14:09

Klara