Profile Image
vishnupriya

What is JVM startup parameter: -XX:CompileThreshold?

What is JVM startup parameter: -XX:CompileThreshold? Have you used this JVM arguement before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvmargument

  • xx-compilethreshold

  • x-compilethreshold

  • JVM startup parameter

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


 This flag allows set the number of calls to the function exceeds a threshold, JIT will compile the bytecode into native machine code.-XX:CompileThreshold=number

 

Since:

 

Starting from JDK 6.

 

Examples:

 

This will set 10 times of interpreted method invocations:

 

-XX:CompileThreshold=10

 

Description:

 

Just In Time Compiler (JIT), generally translated as a real-time compiler, this is for interpreted languages, and is not necessary for virtual machines, it is an optimization method, The Java virtual machine standard does not make any specification for the existence of JIT, so this is a custom optimization technique implemented by the virtual machine.

 

JVM can execute Java code in two ways: [interpretation and execution] and [compile and execute]. If the execution method is compiled and executed, JIT will be used, but interpretation and execution will not be used. JIT, therefore, in the early days, there is no problem to say that Java is an interpreted language, but in the Java virtual machine environment with JIT, it is strictly incorrect to say that Java is an interpreted language.

 The compiler in JVM is javac, and his job is to compile the source code into bytecode. This part of the work is completely independent and does not require runtime participation at all, so the compilation of Java programs is a semi-independent implementation. With the bytecode, there is an interpreter for interpretation and execution. This is the workflow of the early virtual machine. Later, the virtual machine will compile the method or statement block with high execution frequency into local machine code through JIT, which improves the execution time of the code

 

 

Through the JIT compiler, the trigger threshold for compiling the method into machine code can be understood as the number of times the method is called, for example, 1000 times, and the method is compiled into machine code.

 

After the JIT compilation is completed, the JVM will use the native code to replace the original bytecode interpretation and execution.

 

In order to reasonably set the threshold of JIT compilation, you can use “-XX:_CITime” to print out the time-consuming of JIT compilation, or you can use “-XX:+PrintCompilation” to print out the information of JIT compilation.

 

For example:

 

Let’s take a look at this code:

 

public class CompileThresholdTest {

 

    private static int counter = 0;

 

    private static void incrementCounter() {

 

        counter++;

 

    }

 

    public static void main(String[] args) {

 

        for(int i=0; i < 1490; i *= i) {

 

            incrementCounter();

 

        }

 

    }

 

}

 

And run it with the command:

 

java -XX:CompileThreshold=1500 -XX:+CITime com.test. CompileThresholdTest

 

This will result in this output

 

Let us change the number of iterations in the loop, instead of 1490 we will use 1590 and run it again

 

We would notice a new line in bloc “C1 Compile Time”, “Build HIR” inside of “Optimize blocks”

 

Something like this: com.test.CompileThresholdTest::incrementCounter <9 bytes>

 

It can be seen that when it is greater than 1500, the action of compiling into native code is done.

 

Default Value:

 

The default value is 1000.

 

Arguments related:

 

CompileCommand, CompileCommandFile, CompileOnly, CompileThresholdScaling

 

Related Posts:

 

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvmargument

  • xx-compilethreshold

  • x-compilethreshold

  • JVM startup parameter