Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes a JRE 6 JVM code cache leak?

Since switching to JRE 6, my server's code cache usage (non-heap) keeps growing indefinitely. My application creates a lot of classes at runtime, BUT these classes are successfully unloaded during the GC process. I can see these classes getting unloaded in the gc logs and also the permGen usage stays constant. I specifically make sure in my code that these classes are orphaned once I am finished with them and so they correctly get garbage collected from permGen.

The code cache however keeps growing. I only became aware of the code cache after switching to JRE 6. So I guess my questions are:

  1. Does GC include the code cache?
  2. What could cause a code cache memory leak, specifically.
  3. Is there a bug in JDK 6 in this area?
like image 475
Arturo Knight Avatar asked Oct 18 '09 21:10

Arturo Knight


People also ask

What causes memory leaks in Java?

In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.

What causes memory leaks?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.

What is JVM code cache?

What Is the Code Cache? Simply put, JVM Code Cache is an area where JVM stores its bytecode compiled into native code. We call each block of the executable native code a nmethod. The nmethod might be a complete or inlined Java method. The just-in-time (JIT) compiler is the biggest consumer of the code cache area.


2 Answers

You may want to look through this discussion and just go backwards to see what may be helpful in trying to narrow this down: http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/2009-January/000530.html

This one involves JDK5 but may be helpful: http://www.nabble.com/Java-code-cache-memory-td22202283.html

Are you using this to compile jsp pages, or something similar? If not, what is being compiled after the application starts up? Are you using AspectJ with runtime weaving?

It would help to know what you are doing to get a better idea as to how to help.

Also, when the code cache is exhausted, does it just stop compiling anew or does the jvm crash? I would expect the former.

Are you using Sun's JDK? I am guessing you are since I doubt the others are listed as version 6, but it doesn't hurt to ask.

like image 50
James Black Avatar answered Oct 11 '22 17:10

James Black


Two possible places i would look for the problem:Leaked Loaders and Massive String interning.

From your app description, i would more likely verify whether old generated classes are being unloaded by the GC.

I dont think jConsole has a way of visualizing this, but if you can get a copy of YourKit, it has a graphical way of detecting both the above problems.

like image 33
Chander Shivdasani Avatar answered Oct 11 '22 17:10

Chander Shivdasani