Any way to boost JVM Startup Speed?

后端 未结 6 862
耶瑟儿~
耶瑟儿~ 2020-12-10 02:25

It is said that Java is 10x faster than python in terms of performance. That\'s what I see from benchmarks too. But what really brings down Java is the JVM startup time.

6条回答
  •  盖世英雄少女心
    2020-12-10 02:49

    I refer you to Matthew Gilliard's (mjg) blog post on the topic. Any code examples below come straight from there. I won't include timing examples partly to keep this short and partly to induce you to visit his page. Matthew works on the Fn Project so he's very interested in figuring out how to keep startup times low.

    Apparently there are a few ways to do it, and some are pretty easy as well. The core idea is that you cache the JVM's initialization cycle instead of executing it on every startup.

    Class Data Sharing (CDS)

    CDS caches the deterministic (hardware dependant) startup process of the JDK. It's the easiest and oldest (since 1.5 I believe) trick in the book (and not very well-known).

    From Oracle

    When the JVM starts, the shared archive is memory-mapped to allow sharing of read-only JVM metadata for these classes among multiple JVM processes. The startup time is reduced thus saving the cost because restoring the shared archive is faster than loading the classes.

    You can create the dump manually by running

    ⇒ java -Xshare:dump
    Allocated shared space: 50577408 bytes at 0x0000000800000000
    Loading classes to share ...
    // ...snip ...
    total   :  17538717 [100.0% of total] out of  46272512 bytes [ 37.9% used]
    

    ...and then use it with

    java -Xshare:on HelloJava
    

    AOT: Ahead of Time Compilation (Java 9+)

    From mjg's blog

    Where CDS does some parts of classloading of core classes in advance, AOT actually compiles bytecode to native code (an ELF-format shared-object file) in advance, and can be applied to any bytecode.

    Use SubstrateVM (Java 8+)

    Not in the blog post but demonstrated during the talk he gave a few days ago.

    From the readme:

    Substrate VM is a framework that allows ahead-of-time (AOT) compilation of Java applications under closed-world assumption into executable images or shared objects (ELF-64 or 64-bit Mach-O).

提交回复
热议问题