Why is the JVM slow to start?
Solution 1
Here is what Wikipedia has to say on the issue (with some references).
It appears that most of the time is taken just loading data (classes) from disk (i.e. startup time is I/O bound).
Solution 2
Just to note some solutions:
There are two mechanisms that allow to faster startup JVM. The first one, is the class data sharing mechanism, that is supported since Java 6 Update 21 (only with the HotSpot Client VM, and only with the serial garbage collector as far as I know)
To activate it you need to set -Xshare (on some implementations: -Xshareclasses ) JVM options.
To read more about the feature you may visit: Class data sharing
The second mechanism is a Java Quick Starter. It allows to preload classes during OS startup, see: Java Quick Starter for more details.
Solution 3
Running a trivial Java app with the 1.6 (Java 6) client JVM seems instantaneous on my machine. Sun has attempted to tune the client JVM for faster startup (and the client JVM is the default), so if you don't need lots of extra jar files, then startup should be speedy.
Solution 4
If you are using Sun's HotSpot for x86_64 (64bit compiled), note that the current implementation only works in server mode, that is, it precompiles every class it loads with full optimization, whereas the 32bit version also supports client mode, which generally postpones optimization and optimizes the most CPU-intensive parts only, but has faster start-up times.
See for instance:
- http://en.wikipedia.org/wiki/64-bit#32_vs_64_bit
- http://java.sun.com/docs/hotspot/HotSpotFAQ.html#64bit_compilers
That being said, at least on my machine (Linux x86_64 with 64bit kernel), the 32bit HotSpot version supports both client and server mode (via the -client and -server flags), but defaults to server mode, while the 64bit version only supports server mode.
Solution 5
It really depends on what you are doing during the start up. If you run Hello World application it takes 0.15 seconds on my machine.
However, Java is better suited to running as a client or a server/service which means the startup time isn't as important as the connection time (about 0.025 ms) or the round trip time response time (<< 0.001 ms).
Jegschemesch
Updated on March 31, 2020Comments
-
Jegschemesch about 4 years
What exactly makes the JVM (in particular, Sun's implementation) slow to get running compared to other runtimes like CPython? My impression was that it mainly has to do with a boatload of libraries getting loaded whether they're needed or not, but that seems like something that shouldn't take 10 years to fix.
Come to think of it, how does the JVM start time compare to the CLR on Windows? How about Mono's CLR?
UPDATE: I'm particularly concerned with the use case of small utilities chained together as is common in Unix. Is Java now suitable for this style? Whatever startup overhead Java incurs, does it add up for every Java process, or does the overhead only really manifest for the first process?
-
StaxMan almost 15 yearsOne clarification: it's not just (or perhaps so much) disk I/O, but finding classes from within jars (wars, ears), which are zip archives. Extraction takes CPU, but especially if they are compressed.
-
Brett almost 15 yearsCPU time to decompress is minimal. Indeed, stackoverflow itself uses compression as a performance optimisation, and compression is massively more expensive than decompressions.
-
Brett almost 15 yearsServer HotSpot doesn't do compilation until 10,000 iterations. Client does it at 1,500. However, the server is implemented somewhat differently (two instead of one intermediate representations, IIRC).
-
Bastien Léonard almost 15 yearsCPython compiles Python files into bytecode before executing them, like Java.
-
Sake almost 15 yearsWhatever condition it is, jvm is still the vm that has slowest startup. You may find it fast enough on certain grade of machine and for certain size of application. But still its startup time is comparably inarguably slow.
-
Venedictos almost 15 years@Bastien: there is a big difference between bytecode and machine code. Made up bytecode is much easier than generating machine code.
-
Seun Osewa over 14 years@Tom: CPU time to decompress a 2kb class file: minimal. CPU time to decompress a 30mb class file to read a 2kb class file: significant.
-
josefx about 14 years@Seun a jar file uses zip compression, there is no need to decompress the whole archive for one file, each entry (file) is compressed independent of other entries.
-
intuited over 13 yearsI don't get why they don't keep a persistent (i.e. saved across reboots) cache of commonly-used classes, and just check the timestamps of the source jars when a JVM launches. It seems like they could load that at startup and catch 80% of the classes that end up being used by most apps. Well, I guess they know what they're doing.
-
slezica about 12 years(3 years too late) It can, with a tool like Nailgun!
-
Andy about 9 yearsI think GUI apps are primarily what gave Java the impression of being slow...having to load a buttload of AWT and Swing classes is probably what makes it take at least one second.
-
Vishy almost 9 years@AlexMills it's worth noting that these researchers used a machine from 2006. A dual core i3-4370 might be 4-5x faster. Never the less their analysis is most likely current.
-
Alexander Mills almost 9 yearsmaybe, but I assume only one core is responsible for loading the JVM. I bet a SSD vs HDD would make more difference.
-
Vishy almost 9 years@AlexMills it would if they ran the tests with a flushed cache. I suspect they ran the benchmark from memory but it is hard to say as they only had 1 GB of memory.
-
Dzmitry Lazerka almost 7 years@josefx No need to decompress the whole file -- yes. But for ZIP you still need to read the whole content before the entry in ZIP archive to find the entry you need. And IO is usually much slower than CPU to decompress, so in the end it's still almost as slow as decompressing.
-
Dzmitry Lazerka almost 7 yearsAn example of a truly seekable compression is LZO.
-
josefx almost 7 years@DzmitryLazerka Zip archives end with a central directory entry, which lists both entry names and their offsets. You jump to the end, read through the directory and jump to the entry you need.
-
Dzmitry Lazerka almost 7 yearsHmm, yes, you're right. I have always worked with InputStreams (no seek support), but Zip's central directory is at the end of file.
-
Ludwik almost 4 yearsThe java quick starter link is dead :/