What is java code cache?

Java code cache is an area where JVM stores its byte code compiled into native code. Java 7 introduced the feature of tiered compilation. So compiled byte code from java class files passes the JIT-Compiler. This happens at run time and JIT-Compiler does optimizations. JIT-Compiler translates the byte code into native machine code. Once compiled, java stores the native code into code cache. A further execution benefits from code cache. But sometimes java code code cache becomes full.
Java 7 disables tiered compilation by default. But Java 8 changed default to enabled.

OpenJDK 64-Bit Server VM warning: CodeCache is full. Compiler has been disabled.
OpenJDK 64-Bit Server VM warning: Try increasing the code cache size using -XX:ReservedCodeCacheSize=
CodeCache: size=245760Kb used=243859Kb max_used=243859Kb free=1900Kb
bounds [0x00007f8c48df7000, 0x00007f8c57df7000, 0x00007f8c57df7000]
total_blobs=48750 nmethods=47711 adapters=938
compilation: disabled (not enough contiguous free space left)

 

Tuning java code cache

Java code cache has limited size. Once java code cache is full, no native code becomes compiled as JVM disables JIT-Compiler. As result, application performances degrades. But how to avoid this? One option is to flush the code cache. Another option is to increase cache size. The official docs explains all available switches. But at least Open JDK seems to use different defaults. According to official docs, Java 8 enables -XX:+UseCodeCacheFlushing. In this way a full cache java should flush cache. Instead JIT-Compiler becomes disabled.
Up to early Java 8 versions (less update 20) had several issues disabling JIT-Compiler after flushing cache. There is no easy answer. Some guys advise to enable flushing while others recommend to increase cache size.

Use JVM switch –XX:+PrintCodeCache JVM option to debug code cache. Once enabled, JVM produces console output like this:

CodeCache: size=32768Kb used=542Kb max_used=542Kb free=32226Kb

Measure how long it takes until java code cache runs full. Is it hours, days or weeks? Decide if increasing java code cache size may be helpful and costs are reasonable. If applicable, add java code cache to your monitoring. Consider upgrading to newer JVM release.

Conclusion

Keep an eye on your code cache and console notifications. Try out what is the best solution for your problem. Be aware of future changes to run time defaults or newer optimization features.