MWB
MWB

Reputation: 12567

Is it possible to keep JVM in memory between application launches?

Java documentation says:

Small utility applications that run only for a short time may suffer a performance hit if the JVM and Java application startup time is long.

I wonder if it's possible to keep the JVM (not its "state") in memory between application launches to avoid the startup delay?

EDIT I don't think the 2 people who voted to close this question even read it. The JVM and the JVM state are two different things. I asked about keeping the JVM in memory, not saving the JVM state to a file, like the other question. The goal of the former is to save startup time. The latter doesn't do that at all.

Upvotes: 3

Views: 340

Answers (2)

Holger
Holger

Reputation: 298143

Note that the cited sentence is only a conditional (having an “if” in-between) and, omitting technical terms, basically says:

… that run only for a short time may suffer … if … startup time is long.

Obviously, this statement is so fundamental (true but trivial), that you could apply it to everything, including your car or TV. Back in the times when switching on the TV implied heating up the tubes, watching a short may suffer from the long startup time.

At no point, this statement says that there is such a problem. So you are not only drawing the false conclusion that there was such a problem, by asking whether “it’s possible to keep the JVM in memory” you are implying that this problem (if it existed) is caused by the JVM not being kept in memory, without any support for this assumption.

“The JVM” consists of several components. The native code, usually delivered as native shared libraries, is loaded and kept in memory by the operating system. The classes of the JRE are stored in a shared archive which will be memory mapped, hence, shared between JVM instances and kept in memory similar to the native libraries that make up the JVM, if the operating system decides that there is enough unused memory for that.

Other resources and the application’s code are read in using conventional I/O, still, the operating system layer will utilize unused memory for buffering. The difference to the memory mapped libraries and shared class data is that there will be a necessary copying step when reading from an already cached file. But, of course, this is still faster than actually reading from a hard drive.

All in all, “keeping the JVM in memory” is already a fact, regarding those parts that can be kept and reused, and the improvement potential is low. You can easily verify it by starting a small application (e.g. hello world) on a freshly booted computer, then run the same app again using the same JVM and you will notice a significant reduction of the startup time on the second run.

You might consider the startup time of the second run still high compared to a hello world using, e.g. a scripting language, but that’s not an issue of keeping or not keeping JVM components in memory. It’s also highly subjective, which startup time to consider long enough to consider the application suffering from it.

Upvotes: 0

JQian
JQian

Reputation: 226

This depends how you want to use the JVM. If last JVM job finishes and no data there needed, you can reuse JVM for next or future jobs. This has been deployed in big data applications to shorten JVM startup delay (e.g. Hadoop or Spark), and you can refer to this for more information.

Upvotes: 0

Related Questions