Is your Java code friendly to JIT compilation ?, Java code jit compilation

Source: Internet
Author: User

Is your Java code friendly to JIT compilation ?, Java code jit compilation

JIT compiler is one of the most efficient and important components of a Java Virtual Machine (JVM. However, many programs do not make full use of JIT's high-performance optimization capabilities, and many developers do not even know the extent to which their programs use JIT effectively.

 

In this article, we will introduce some simple methods to verify whether your program is JIT-friendly. Here we do not intend to cover details such as how the JIT compiler works. It only provides some simple basic detection and methods to help your code be JIT-friendly and optimize it.

The key to JIT compilation is that the JVM automatically monitors the methods being executed by the interpreter. Once a method is considered to be frequently called, the method will be marked and compiled to the local machine commands. Compilation of these frequently executed methods is completed by a JVM thread in the background. Before the compilation is complete, JVM will execute the interpretation version of this method. Once the method is compiled, JVM replaces the version interpreted by this method in the method scheduling table with the compiled version.

 

Hotspot virtual machines have many JIT compilation optimization technologies, but the most important one is inline. During the inline process, the JIT compiler effectively extracts the method body of a method to its caller, thus reducing virtual method calls. For example, see the following code:

public int add(int x, int y) {    return x + y;}int result = add(a, b);

When inline occurs, the above Code will become

Related vendor content

Experiment and practice-how can small and medium-sized enterprises, such as Cui baoqiu, Chi Jianqiang, and Zhou aimin, who are co-chairs of ArchSummit, deploy elastic private cloud at a low cost? How to design a high-availability communication system to cope with sudden increases in user volume?
int result = a + b;

The preceding variables a and B Replace the parameters of the method, and the method body of the add method has been copied to the caller's region. Using inline can bring many benefits to the program, such

 

  • No extra performance loss
  • Reduce indirect pointer references
  • No virtual Method Search for inline Methods

In addition, by copying the implementation of the method to the caller, the JIT compiler processes more code, making subsequent optimization and more inline possible.

Inline depends on the size of the method. By default, inline operations can be performed with 35 bytecode or less. For frequently called methods, the critical value can reach 325 bytes. You can set the-XX: MaxInlineSize = # option to modify the maximum critical value. You can set the limit XX: FreqInlineSize = # option to modify the critical value of frequently called methods. However, we should not modify these configurations without correct analysis. Blind modification may have an unpredictable impact on the program's performance.

Since inline can greatly improve the Code performance, it is particularly important to allow as many methods as possible to achieve inline conditions. Here we will introduce a tool called Jarscan to help us detect how many methods in the program are good for our internal friends.

The Jarscan tool is part of the JITWatch Open Source Tool suite compiled by JIT. Unlike the main tool for analyzing JIT logs at runtime, Jarscan is a tool for Static Analysis of jar files. The output result format of the tool is CSV. The result contains information such as methods that exceed the critical value of frequently called methods. JITWatch and Jarscan are part of the AdoptOpenJDK project, headed by Chris Newland.

Before using Jarscan and obtaining analysis results, download the binary tool (Java 7 and Java 8) from the AdoptOpenJDK Jenkins website ).

The operation is simple, as shown below:

./jarScan.sh <jars to analyse>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.