[Java Application Performance and Memory Management] Chapter 2- Just In Time Compilation and the Code Cache (2)

Published by

on

C1 compiler and C2 compiler

There are two compilers inside the JVM JIT compilers.

  • C1 compiler does level 1~3 compilation, and C2 compiler does the level 4 compilation
    • This level is called as ‘compilation tier
    • The higher the level is, the more optimized the code would be. C2 compiler optimizes more than C1 compiler.
  • JVM decides which compiler to use according to how often it is run and complexity / time of the code.
    • This is called as “Profiling
    • Normally it uses C1 compiler, and then uses C2 when the level becomes 4. Not all of the codes will be level 4, even though it is frequently used. The frequently called & complex jobs will be level 4.
  • JVM also decides to put the code into code cache if the code is being run so often.
    • Code cache is a special area of memory, which makes it faster to access.

java -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation <CLASS_NAME> <PARAMS>

With the -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation options, we can check isPrime method from the Main class gets into level 4 as it gets called more and more.

Code Cache

Code cache has a limited size. So if lots of codes are being saved in this memory, some of the codes will be removed out and then put back in when necessary. Many move in-out occurs from the code cache.

  • -XX:+PrintCodeCache: Print the size of code cache
    • Code cache size differs per java version. Java 8 + 64bit JVM has 240MB max size
  • -XX:InitialCodeCacheSize: Differs per computer memories, but mostly it is 160KB.
  • -XX:ReservedCodeCacheSize: Max code cache size that can be automatically increased by the program itself.
  • -XX:CodeCacheExpansionSize: Size that can be increased at a time.

Jconsole

To check the code cache usage from the remote server, you can use Jconsole . Jconsole is included in the JDK, so you can just run jconsole from the Mac/Linux terminal. (Windows require more work to do)

  • However, it is not recommended to use jconsole from the remote server. This is because, VM requires more resources to get jconsole work.
    • In other words, using jconsole consumes more memory(approximately 2MB of code cache), which can affect the remote server that’s running the application.

Reference

Leave a comment