Quick revision
The short answer
The code cache is the native memory area where HotSpot keeps executable machine code: the code the JIT compilers produce for hot methods, plus the interpreter and small helper routines called stubs. Since JDK 9 it is segmented into a non-method heap, a profiled heap for C1 code and a non-profiled heap for fully optimised code, and its maximum is set with ReservedCodeCacheSize. We can see how full it is with jcmd <pid> Compiler.codecache or through the JMX memory pools named CodeHeap, and it matters because a full code cache stops the JIT from compiling new methods.
What to remember
In this answer, we saw that the code cache holds the machine code produced by the C1 and C2 compilers, together with the interpreter, stubs and adapters. Since JDK 9 it is split into non-method, profiled and non-profiled segments to reduce fragmentation, ReservedCodeCacheSize sets its maximum, and jcmd Compiler.codecache or the CodeHeap memory pools show how full it is. When it fills, the JIT stops compiling and the application slows down.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The code cache is the native memory area where HotSpot keeps executable machine code: the code the JIT compilers produce for hot methods, plus the interpreter and small helper routines called stubs. Since JDK 9 it is segmented into a non-method heap, a profiled heap for C1 code and a non-profiled heap for fully optimised code, and its maximum is set with ReservedCodeCacheSize. We can see how full it is with jcmd <pid> Compiler.codecache or through the JMX memory pools named CodeHeap, and it matters because a full code cache stops the JIT from compiling new methods.
Why this question is asked
Most Java developers never think about the code cache until a warning in the log says it is full. Yet it is where the speed of a warmed-up Java service actually lives, because the machine code there is what makes Java fast after startup. Interviewers ask this to see whether we understand the JIT well enough to know where its output goes and what happens when that space runs out. In this answer, we will recap how the JIT produces code, look at what the code cache holds and how it is divided, see the flags that size it, and learn how to check its usage.
1A Quick Recap of the JIT
Java code starts in the interpreter, which executes bytecode one instruction at a time. That is slow but needs no preparation. As methods are called often, HotSpot compiles them to machine code with its just-in-time compilers. C1 compiles quickly and adds profiling, recording which branches are taken and which types appear. C2 later uses that profile to produce highly optimised code for the hottest methods. This step-by-step approach is called tiered compilation, and it is on by default.
Compiled code has to live somewhere the CPU can execute it. That place is the code cache.
2What the Code Cache Holds and How It Is Divided
The code cache holds three kinds of code. Compiled methods, called nmethods, are the output of C1 and C2. Non-method code includes the template interpreter itself and stubs, small pieces of machine code the JVM uses for things such as calling native methods or handling type checks. Some adapters convert between interpreted and compiled calling conventions.
Before JDK 9 all of this shared one area, which fragmented as short-lived profiled code was mixed with long-lived optimised code. JEP 197 split it into three segments so each kind of code can be managed separately.
- non-nmethodsInterpreter, stubs, adapters; small and fixed, a few MB
- profiled nmethodsC1 code with profiling; short-lived, replaced as methods get hot
- non-profiled nmethodsC2 code and fully optimised C1 code; long-lived, the fast path
3 layers, from the top down. Step through them one at a time.
Each segment has its own memory pool, so we can see which kind of code is filling up.
3Sizing the Code Cache
ReservedCodeCacheSize sets the maximum size of the whole code cache. With tiered compilation on, the default on 64-bit platforms is 240 MB, which is split between the segments; without tiered compilation it is much smaller. Defaults have changed between releases, so check your version with PrintFlagsFinal. The memory is reserved at startup but committed only as code is added, so a 240 MB reservation does not mean 240 MB of resident memory.
4Seeing How Full It Is
The quickest check is jcmd. It prints each segment's size, how much is used and the largest free block, which tells us about fragmentation.
Notice the line compilation: enabled. If the cache ever fills completely, that line changes and the log says CodeCache is full. Compiler has been disabled. From then on, new hot methods stay in the interpreter or at a low tier, and the application slowly gets slower. A dashboard panel per CodeHeap pool, with an alert at about 90 percent of its size, catches this before it happens.
5Follow-up questions
Does a full code cache crash the JVM?
No. The JVM keeps running with the code it already compiled, but it cannot compile new methods or recompile methods whose optimised code was thrown away, so performance degrades over time. Recent JDKs free unused compiled code during GC, so space can come back, but if usage is genuinely too high, ReservedCodeCacheSize should be raised.
Does the code cache count against a container memory limit?
Yes. It is native memory, so its committed size counts towards the process's resident memory. It appears in native memory tracking under the Code category and should be part of the memory budget.
Why are profiled and non-profiled code kept apart?
Because they live for different lengths of time. Profiled C1 code is a stepping stone that is replaced once C2 compiles the method, while C2 code stays for a long time. Keeping them in separate segments stops short-lived code from fragmenting the area used by long-lived code.
6Conclusion
In this answer, we saw that the code cache holds the machine code produced by the C1 and C2 compilers, together with the interpreter, stubs and adapters. Since JDK 9 it is split into non-method, profiled and non-profiled segments to reduce fragmentation, ReservedCodeCacheSize sets its maximum, and jcmd Compiler.codecache or the CodeHeap memory pools show how full it is. When it fills, the JIT stops compiling and the application slows down.
Open with
The code cache is the native memory area where HotSpot keeps executable machine code. Most of it is the output of the JIT compilers: C1 code with profiling and C2's optimised code for hot methods.
Then, if they want more
It also holds the template interpreter, stubs, which are small helper routines, and adapters between interpreted and compiled code. Since JDK 9 it is segmented into three heaps: non-nmethods for the interpreter and stubs, profiled nmethods for C1 code, and non-profiled nmethods for C2 code.
They are separate because profiled code is short-lived and C2 code is long-lived, so mixing them fragmented the old single area. ReservedCodeCacheSize sets the maximum; with tiered compilation the default is a few hundred megabytes, 240 MB on the versions I have checked, but I would confirm with PrintFlagsFinal.
It is reserved up front but only committed as code is added. To see usage I run jcmd with Compiler.codecache, which prints each segment's size, used, max used and free space, plus whether compilation is still enabled. In monitoring, the JMX memory pools named CodeHeap show the same numbers.
It matters because when the cache fills, the JIT stops compiling new methods and the application slowly gets slower, so I would alert at around 90 percent.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the code cache in HotSpot, what is stored there, and how can you see how full it...": https://scrivio-interview-library.netlify.app/java/memory-layout/e14243711f/