Java8 metaspace & heap usage

半世苍凉 提交于 2019-12-03 10:16:51

Run jmap -histo PID to see which objects consume the Heap Space.
When I ran your example I saw the heap full of Javassist auxiliary objects:

 num     #instances         #bytes  class name
----------------------------------------------
   1:        592309      312739152  [Ljavassist.bytecode.ConstInfo;
   2:       6515673      208501536  java.util.HashMap$Node
   3:       2964403      169188824  [C
   4:       1777622      102165184  [Ljava.lang.Object;
   5:       4146200       99508800  javassist.bytecode.Utf8Info
   6:       3553889       85293336  java.util.ArrayList
   7:       2964371       71144904  java.lang.String
   8:        593075       56944008  java.lang.Class
   9:        592332       47388032  [Ljava.util.HashMap$Node;
  10:        592309       37907776  javassist.bytecode.ClassFile
  11:        592308       37907712  javassist.CtNewClass
  12:       1185118       28555808  [B
  13:        592342       28432416  java.util.HashMap
  14:       1184624       28430976  javassist.bytecode.ClassInfo
  15:        592309       28430832  [[Ljavassist.bytecode.ConstInfo;
  16:        592322       23692880  javassist.bytecode.MethodInfo
  17:        592315       23692600  javassist.bytecode.CodeAttribute
  18:        592434       18957888  java.util.Hashtable$Entry
  19:        592309       18953888  javassist.bytecode.ConstPool
  20:        592308       18953856  java.lang.ref.WeakReference
  21:        592318       14215632  javassist.bytecode.MethodrefInfo
  22:        592318       14215632  javassist.bytecode.NameAndTypeInfo
  23:        592315       14215560  javassist.bytecode.ExceptionTable
  24:        592309       14215416  javassist.bytecode.LongVector
  25:        592309       14215416  javassist.bytecode.SourceFileAttribute
  26:        592507        9487584  [I
  27:             8        6292528  [Ljava.util.Hashtable$Entry;
  28:           212          18656  java.lang.reflect.Method
  29:           407          13024  java.util.concurrent.ConcurrentHashMap$Node
  30:           124           8928  java.lang.reflect.Field

what exactly makes this increase in heap as it loads more classes into Metaspace ?

My hypothesis is that this is "ordinary" garbage that is being created by your example. I surmise that:

  • The javaassist code creates regular heap objects. They are mostly "big" and that causes them to be allocated directly into the OldGen heap. Or something else causes that.

    (UPDATE - looking at @apangin's Answer, I now suspect that they started out in the YoungGen heap and were tenured ...)

  • When classLoader.defineClass is called under the hood, it creates objects in metaspace from the byte array containing the classfile.

  • The OldGen usage remains ... because nothing has triggered a full GC yet.

If you tweaked your example so that the classes were reachable, and then forced a full GC, I would expect (hope) to see the OldHeap usage to drop, indicating that it is "ordinary" garbage rather than a storage leak.

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