Even if a JIT compiler is declared to be used, it is not necessarily the specified compiler, depending on the runtime platform. JIT Compiler Optimization (advanced tutorial) For most scenarios, it is sufficient to set only the JIT compiler:-client,-server or-XX: + TieredCompilation. For long-running applications, the Tiered compilation method is better. Even if you use it for short-running references, the performance is similar to that of using the Client compiler.
However, in other cases, we still need to make some optimizations.
Code Cache Optimization) After the JVM compiles the Code, the compiled Code is stored in the Code Cache in the form of assembly instructions. Obviously, this Cache area also has a size limit, when this area is filled up, the compiler cannot compile other Java bytecode.
So when this area is set too small, it will affect program performance, because the compiler will not compile Java bytecode to get faster compilation instructions/binary code.
This effect is more common when Tiered is used to compile the policy. Because this policy is running, the compiler behavior is similar to the Client compiler. At this time, a large number of Java bytecode will be compiled. If the Code Cache is set too small, the performance will not be fully improved.
When the Code Cache area is filled up, the JVM will give a warning:
Java HotSpot (TM) 64-Bit Server VM warning: CodeCache is full. compiler has been disabled. java HotSpot (TM) 64-Bit Server VM warning: Try increasing the code cache size using-XX: ReservedCodeCacheSize =
Of course, you can also know the usage of Code Cache by viewing the compilation log.
Relationship between the Java platform and the default Code Cache space:
Java platform |
Default Space |
32-bit client, Java 8 |
32 MB |
32-bit server with tiered compilation, Java 8 |
240 MB |
64-bit server with tiered compilation, Java 8 |
240 MB |
32-bit client, Java 7 |
32 MB |
32-bit server, Java 7 |
32 MB |
64-bit server, Java 7 |
48 MB |
64-bit server with tiered compilation, Java 7 |
96 MB |
In Java 7, the default space in this region is often insufficient. Therefore, you need to increase the space needed in necessary scenarios. However, there is no good way to show whether an application can achieve the best performance only when the space of the Code Cache area is large. All you can do is keep trying to get the best result.
The maximum space of Code Cache can be obtained through:-XX:ReservedCodeCacheSize=N. By default, N is the default space in the table above. The space management of Code Cache is similar to that of other memory spaces in JVM. It also provides a method to set the initial value:-XX:InitialCodeCacheSize=N. The initial value is related to the type of the compiler and the architecture of the processor. However, you need to set only the maximum space.
Size of the Code cache space So is it better to set the larger the Code Cache space? Not all. After the configuration, even if it is not actually used, this space is also "scheduled" by JVM and cannot be used as another path.
As mentioned earlier, if the JVM uses 32-bit memory, the maximum memory space is 4 GB, which includes the Java heap, JVM Code (including various Native libraries and thread stacks used), memory space that can be allocated by applications, and Code Cache. From this point of view, it is not set to the larger the better.
You can use the jconsole tool to monitor the program running.
Reserved Memory and allocated Memory (Reserved Memory and Allocation Memory) They are two very important concepts in JVM, and will appear in Code Cache, Java heap, and various JVM memory areas.
Summary
- The Code cache affects the number of Java bytecode that can be compiled by JVM. It sets a maximum available space. When the space is fully occupied, the JIT compiler stops compilation.
- When using the Tiered compilation policy, the Code cache will soon be used up (especially Java 7). At this time, you can adjust it by setting the reserved space (that is, the maximum available space.