Sub topic 14
The Code Cache
Assume you know basic Java, can run java from a command line, and understand that the JVM can compile frequently used bytecode into native machine code. This chapter explains where that compiled code lives and what happens when the available space becomes limited.
What the code cache stores
The HotSpot JVM stores compiled native methods in a memory area called the code cache. A compiled method is often called an nmethod. It contains machine code plus metadata that the JVM needs for tasks such as stack walking, deoptimization, and garbage collection.
The code cache is separate from the Java heap. Creating more Java objects does not directly consume code-cache space. However, loading classes, compiling methods, and repeatedly replacing compiled versions can increase code-cache use.
The cache can contain:
- Compiled methods produced by C1 and C2.
- Small JVM-generated stubs.
- Adapter code that connects different calling conventions.
- Metadata associated with compiled methods.
- Code that is still valid, code waiting to be removed, and code that is no longer usable.
In JDK 11, a tiered HotSpot VM normally uses a segmented code cache. Different kinds of code are placed in separate logical regions.
Reserved and used space
HotSpot reserves a maximum amount of virtual memory for the code cache. It commits physical memory as needed. The important distinction is:
- Reserved is the maximum address space available.
- Committed is memory currently made available for use.
- Used is memory occupied by code and related data.
The main size-related flags include:
# Display code-cache information when the JVM exits
java -XX:+PrintCodeCache -version
# Set the maximum total code-cache size
java -XX:ReservedCodeCacheSize=256m MyApplicationReservedCodeCacheSize controls the maximum size, not the amount immediately committed. The default depends on the JVM and platform. Check the output of your specific JDK rather than assuming a fixed default.
Reclamation and full-cache behavior
Compiled code can become invalid after speculative assumptions fail. For example, a method may be compiled after HotSpot observes only one implementation of an interface. If another implementation later appears, the compiled method might be made not entrant and eventually removed.
A background code-cache sweeper finds obsolete compiled methods. This reclamation allows the JVM to reuse space. The JVM cannot remove a method while a thread is actively executing it, so removal can occur in stages.
If the cache fills faster than HotSpot can reclaim code, the JVM may stop compiling additional methods. The application can continue running interpreted or already compiled code, but performance may decline. A message such as CodeCache is full in diagnostic output indicates that the cache limit deserves investigation.
Practical diagnosis
Use PrintCodeCache for a summary and PrintCompilation to connect cache growth with compilation activity:
java -XX:+PrintCompilation -XX:+PrintCodeCache MyApplicationFor production analysis, also consider the unified logging available in JDK 11:
java -Xlog:codecache=debug MyApplicationThese options are diagnostic aids. Measure the application under a representative workload because a short startup run may not compile the same methods as a long-running service.
Exercise
Why does making the Java heap larger usually not solve a full code cache?
Show answer
The heap stores Java objects, while the code cache stores compiled native methods and related metadata. They are separate memory areas with separate limits.