Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diagnose a Java 8 metaspace leak?

I have a J2EE application with some interesting behavior ... the heap seems to behave well, growing and shrinking with garbage collections as expected over time. There is no appreciable overall long term heap expansion. However, the metaspace just keeps steadily growing at about 20 Mb per hour until we hit MaxMetaspace and encounter an OOME. I have tried both the parallel and G1 garbage collectors (jdk1.8.0_40).

The application is not getting re-deployed during the execution, so it doesn't seem like it would be the typical classloader leak. Does anyone have suggestions as to how to track down the source of this leak?

like image 244
It Worked Yesterday Avatar asked Apr 02 '15 22:04

It Worked Yesterday


People also ask

How do I fix Java Lang OutOfMemoryError Metaspace?

OutOfMemoryError: Metaspace error is thrown. To mitigate the issue, you can increase the size of the Metaspace by adding the -XX:MaxMetaspaceSize flag to startup parameters of your Java application. For example, to set the Metaspace region size to 128M, you would add the following parameter: -XX:MaxMetaspaceSize=128m .

Is Metaspace added in Java 8?

MetaSpace grows automatically by default. Here, the garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size. It is removed from java 8. It is introduced in Java 8.

Do we have PermGen in Java 8 Are you aware of Metaspace?

Until Java 7 there was an area in JVM memory called PermGen, where JVM used to keep its classes. In Java 8 it was removed and replaced by area called Metaspace.


1 Answers

The main cause for the java.lang.OutOfMemoryError: Metaspace is:

  • either too many classes or
  • too big classes being loaded to the Metaspace.

If you want to recreate the problem use this code snippet:

public class Metaspace { static javassist.ClassPool cp = javassist.ClassPool.getDefault();  public static void main(String[] args) throws Exception {     for (int i = 0; ; i++) {          Class c = cp.makeClass("eu.plumbr.demo.Generated" + i).toClass();     }   } } 

All those generated class definitions end up consuming Metaspace.

Javaassist in Maven repo.

You can find a lot more about OOME here

like image 77
Karol Król Avatar answered Sep 28 '22 08:09

Karol Król