How to diagnose a Java 8 metaspace leak?

和自甴很熟 提交于 2019-12-03 14:29:01

问题


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?


回答1:


Do a heap dump and analyze it with Eclipse MAT. Look at the classes you have loaded. Check if there's something unexpected, especially duplicate classes. It also has a classloader explorer.

Edit: In theory you could also be that you're constantly generating proxies.




回答2:


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



来源:https://stackoverflow.com/questions/29423390/how-to-diagnose-a-java-8-metaspace-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!