Backend Development 5 min read

Understanding JVM Hotspot Compilation and Performance Gains in Java

The article explains how the JVM selectively compiles frequently executed Java code into optimized native code, demonstrates significant performance improvements through JIT compilation with two demo programs, and highlights the trade‑offs between interpretation and compilation for hotspot methods.

FunTester
FunTester
FunTester
Understanding JVM Hotspot Compilation and Performance Gains in Java

For a program, only a portion of the code is executed frequently, and the application’s performance largely depends on how fast this hotspot code runs; such frequently executed sections are called hot code.

The JVM does not blindly compile every method: if a method is executed only once, interpreting the bytecode is faster than compiling it, but for methods called repeatedly, compilation is worthwhile because the compiled code runs faster and the accumulated time saved outweighs the compilation cost.

This trade‑off explains why the compiler initially interprets code—it can identify methods that are invoked often enough to merit compilation, and it can apply extensive optimizations once it has gathered execution statistics.

Demo 1 runs identical code and shows a performance improvement of roughly 1/16 of the original execution time.

package com.fun;

import com.fun.frame.SourceCode;

class TSSS extends SourceCode {
    public static void main(String[] args) {
        def mark0 = getNanoMark();
        100.times {
            getPercent(23, 17)
        }
        def mark1 = getNanoMark()
        output(getFormatNumber(mark1 - mark0))

        def mark10 = getNanoMark();
        100.times {
            getPercent(23, 17)
        }
        def mark11 = getNanoMark()
        output(getFormatNumber(mark11 - mark10))
    }
}

Console output for Demo 1:

INFO-> 当前用户:fv,IP:192.168.0.100,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.3
INFO-> 161,422,326
INFO-> 10,559,361
Process finished with exit code 0

Demo 2 runs the same method with random parameters and achieves a performance improvement of about 1/2 of the original time.

package com.fun;

import com.fun.frame.SourceCode;

class TSSS extends SourceCode {
    public static void main(String[] args) {
        def mark0 = getNanoMark();
        100.times {
            getPercent(getRandomInt(100), getRandomInt(30))
        }
        def mark1 = getNanoMark()
        output(getFormatNumber(mark1 - mark0))

        def mark10 = getNanoMark();
        100.times {
            getPercent(getRandomInt(100), getRandomInt(30))
        }
        def mark11 = getNanoMark()
        output(getFormatNumber(mark11 - mark10))
    }
}

Console output for Demo 2:

INFO-> 当前用户:fv,IP:192.168.0.100,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.3
INFO-> 197,810,630
INFO-> 9,012,579
Process finished with exit code 0

The article concludes with a disclaimer that the content was originally published on the “FunTester” public account and should not be reproduced by third parties except Tencent Cloud.

JavaJVMPerformanceOptimizationJITHotSpot
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.