How to diagnose a Java 8 metaspace leak?

后端 未结 2 1011
猫巷女王i
猫巷女王i 2021-02-06 21:42

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 app

2条回答
  •  广开言路
    2021-02-06 22:35

    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

提交回复
热议问题