4 Compilation Optimization

This topic describes the various compiler options available in Oracle JRockit and HotSpot to optimize compilation.

Compiler Considerations

Unlike Oracle JRockit, HotSpot features a Java byte code interpreter in addition to two different Just In Time (JIT) compilers, client (also known as C1) and sever (also known as C2).

This section provides details about the complier that you can use.

HotSpot VM defaults to interpreting Java byte code. It compiles (JIT compilation) methods that are executed for a predetermined number of times. JIT compliers are either client or server compilers.

  • Client compiler: It compiles methods quickly but emits machine code that is less optimized than the server compiler. This complier is used for quick startup. Also, in this compiler, the smaller memory footprint is more important than steady-state performance.

  • Server compiler: The compiler often takes more time (and memory) to compile the same methods. However, it generates better optimized machine code than the code generated by the client compiler. It provides better runtime performance after the application reaches the steady state.

The tiered compilation enhances the server VM startup speed by using client compiler as the first tier. A server VM uses the interpreter to collect the profiling information about the methods that is fed into the compiler. In the tiered scheme, in addition to the interpreter, the client compiler generates compiled versions of methods that collect profiling information about themselves. As the compiled code is substantially faster than the interpreter, the program executes with greater performance during this profiling phase. Often, even a startup is faster because the final code produced by the server compiler is available during the early stages of application initialization. The tiered scheme can also achieve better peak performance than a regular server VM. This is because the faster profiling phase allows a longer period of profiling, which yields better optimization.

Tiered compilation is the default mode for the server VM. The 64-bit mode is supported. To enable tiered compilation manually, use the -XX:+TieredCompilation flag. You can disable tiered compilation by using the -XX:-TieredCompilation flag.

Oracle JRockit JVM compiles a Java method and generates the machine code for the first time it is invoked. This compiled code of frequently invoked methods is optimized in the background by an Optimizer thread. This code is different from the HotSpot where methods are interpreted first and compiled later, either by the client (fewer optimizations) or the server (more optimizations) compiler.

Compiler Control

Compiler Control provides an improved way to control the JVM compilers. The JVM compilation is done through compiler directive options. These options are method-specific and can be changed at run time. See Compiler Control.

Important HotSpot JIT Compiler Options

The following table lists some important Oracle JRockit and HotSpot compiler options:

Table 4-1 JIT Compiler Options

Oracle JRockit HotSpot Notes

-XnoOpt

-XXoptFile:<file>

As JIT compilation in HotSpot is considered analogous to optimization in Oracle JRockit (that is, both techniques are only used on methods that are determined by profiling to be hot), the HotSpot equivalent to Oracle JRockit's -XnoOpt is -Xint. In this technique, no JIT compilation is done and only the byte code interpreter is used to execute all methods. This compilation might impact the performance. However, it can be useful when-XnoOpt is used for troubleshooting or working around possible compiler issues of Oracle JRockit.

Like Oracle JRockit, HotSpot also offers ways to exclude methods from compilation or to disable specific optimizations on them.

If there are any problems while optimizing the methods, then use XnoOpt or XXoptFile options with Oracle JRockit VM to disable the optimization on those methods. However, to exclude the compilation or disable specific optimizations on these methods, ensure that you don’t directly translate to HotSpot options.

The same compilation or optimization problems observed with the Oracle JRockit JVM for any specific methods are unlikely to happen with the HotSpot JVM. So, to begin with, it is best to remove these options while migrating to the HotSpot JVM.

Equivalent HotSpot JVM options are:

  • -XX:CompileCommand=command,method[,option]

    Specifies a command to perform on a method. For example, to exclude the indexOf() method of the String class from being compiled, use the following:

    -XX:CompileCommand=exclude,java/lang/String.indexOf

  • -XX:CompileCommandFile=<filename>

    Sets the file from which JIT compiler commands are read. By default, the .hotspot_compiler file is used to store commands performed by the JIT compiler.

  • -XX:CompileOnly=<methods>

    Sets the list of methods (separated by commas) to which compilation must be restricted.

  • -XX:CompileThreshold=<invocations>

    Sets the number of interpreted method invocations before compilation. By default, in the server JVM, the JIT compiler performs 10,000 interpreted method invocations to gather information for efficient compilation.

Options CompileCommand, CompileCommandFile, CompileOnly, and CompileThreshold can be used to disable or delay the compilation of specified methods.

-XX:OptThreads

There are no optimization threads in HotSpot JVM. The count of compiler threads that perform both the compilation and the optimizations can be set using:

-XX:CICompilerCount=<threads>

Sets the number of compiler threads to use for compilation. By default, the number of threads is set to 2 for the server JVM and it scales to number of cores if tiered compilation is used.

-XX:+ReserveCodeMemory

-XX:MaxCodeMemory=<size>

-XX:ReservedCodeCacheSize=<size>

Sets the maximum code cache size (in bytes) for JIT-compiled code. This option is equivalent to -Xmaxjitcodesize.

None

-XX:+TieredCompilation

Enables the use of tiered compilation. This option is enabled by default from JDK 8 and later versions. Only the Java HotSpot Server VM supports this option.