16

I was just wondering if it's possible to dump a running Java program into a file, and later on restart it (same machine)

It's sounds a bit weird, but who knows

--- update -------

Yes, this is the hibernate feature for a process instead of a full system. But google 'hibernate jvm process' and you'll understand my pain.

There is a question for linux on this subject (here). Quickly, it's possible to hibernate a process (far from 100% reliable) with CryoPID.

A similar question was raised in stackoverflow some years ago.

With a JVM my educated guess is that hibernating should be a lot easier, not always possible and not reliable at 100% (e.g. UI and files).


Serializing a persistent state of the application is an option but it is not an answer to the question.

Community
  • 1
  • 1
ic3
  • 7,917
  • 14
  • 67
  • 115
  • Do you mean something along the lines of creating a save file? – Max Jan 26 '12 at 19:21
  • Decades ago I remember reading instructions on how you could core dump the awk interpreter while it was running an awk program and then re-launch it from the core file. At the time I thought it was pretty crazy. – Paul Tomblin Jan 26 '12 at 19:22
  • This does sound weird. Are you talking about dumping the source code of a running Java program into a text file, compiling this source code and running it? – Bernard Jan 26 '12 at 19:22
  • Or more like going into hibernate? – vextorspace Jan 26 '12 at 19:22
  • yes, it's like an process hibernate – ic3 Jan 27 '12 at 07:25

10 Answers10

10

This may me a bit overkill but one thing you can do is run something like VirtualBox and halt/save the machine.

There is also:
- JavaFlow from Apache that should do just that even though I haven't personally tried it.
- Brakes that may be exactly what you're looking for

Frankie
  • 24,627
  • 10
  • 79
  • 121
  • This question/answer also suggests going the VM route: http://stackoverflow.com/questions/424341/are-there-any-java-vms-which-can-save-their-state-to-a-file-and-then-reload-that – jefflunt Jan 26 '12 at 19:57
8

There are a lot restrictions any solution to your problem will have: all external connections might or might not survive your attempt to freeze and awake them. Think of timeouts on the other side, or even stopped communication partners - anything from a web server to a database or even local files.

You are asking for a generic solution, without any internal knowledge of your program, that you would like to hibernate. What you can always do, is serialize that part of the state of your program, that you need to restart your program. It is, or at least was common wisdom to implement restart point in long running computations (think of days or weeks). So, when you hit a bug in your program after it run for a week, you could fix the bug and save some computation days.

The state of a program could be surprisingly small, compared to the complete memory size used.

You asked "if it's possible to dump a running Java program into a file, and later on restart it." - Yes it is, but I would not suggest a generic and automatic solution that has to handle your program as a black box, but I suggest that you externalize the important part of your programs state and program restart points.

Hope that helps - even if it's more complicated than what you might have hoped for.

Jörg Beyer
  • 3,631
  • 21
  • 35
7

I believe what the OP is asking is what the Smalltalk guys have been doing for decades - store the whole programming/execution environment in an image file, and work on it.

AFAIK there is no way to do the same thing in Java.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
7

There has been some research in "persisting" the execution state of the JVM and then move it to another JVM and start it again. Saw something demonstrated once but don't remember which one. Don't think it has been standardized in the JVM specs though...

Found the presentation/demo I was thinking about, it was at OOPSLA 2005 that they were talking about squawk

Good luck!

Other links of interest: Merpati

Aglets

M-JavaMPI

Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
3

How about using SpringBatch framework?

As far as I understood from your question you need some reliable and resumable java task, if so, I believe that Spring Batch will do the magic, because you can split your task (job) to several steps while each step (and also the entire job) has its own execution context persisted to a storage you choose to work with.

In case of crash you can recover by analyzing previous run of specific job and resume it from exact point where the failure occurred.

You can also pause and restart your job programmatically if the job was configured as restartable and the ExecutionContext for this job already exists.

Good luck!

aviad
  • 8,229
  • 9
  • 50
  • 98
3

I believe : 1- the only generic way is to implement serialization. 2- a good way to restore a running system is OS virtualization 3- now you are asking something like single process serialization.

The problem are IOs. Says your process uses a temporary file which gets deleted by the system after 'hybernation', but your program does not know it. You will have an IOException somewhere.

So word is , if the program is not designed to be interrupted at random , it won't work. Thats a risky and unmaintable solution so i believe only 1,2 make sense.

2

I guess IDE supports debugging in such a way. It is not impossible, though i don't know how. May be you will get details if you contact some eclipse or netbeans contributer.

Arshed
  • 321
  • 2
  • 11
0

First off you need to design your app to use the Memento pattern or any other pattern that allows you to save state of your application. Observer pattern may also be a possibility. Once your code is structured in a way that saving state is possible, you can use Java serialization to actually write out all the objects etc to a file rather than putting it in a DB.

Just by 2 cents.

Ali
  • 12,354
  • 9
  • 54
  • 83
0

What you want is impossible from the very nature of computer architecture.

Every Java program gets compiled into Java intermediate code and this code is then interpreted into into native platform code (when run). The native code is quite different from what you see in Java files, because it depends on underlining platform and JVM version. Every platform has different instruction set, memory management, driver system, etc... So imagine that you hibernated your program on Windows and then run it on Linux, Mac or any other device with JRE, such as mobile phone, car, card reader, etc... All hell would break loose.

You solution is to serialize every important object into files and then close the program gracefully. When "unhibernating", you deserialize these instances from these files and your program can continue. The number of "important" instances can be quite small, you only need to save the "business data", everything else can be reconstructed from these data. You can use Hibernate or any other ORM framework to automatize this serialization on top of a SQL database.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
0

Probably Terracotta can this: http://www.terracotta.org

I am not sure but they are supporting server failures. If all servers stop, the process should saved to disk and wait I think.

Otherwise you should refactor your application to hold state explicitly. For example, if you implement something like runnable and make it Serializable, you will be able to save it.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385