美文网首页
JVM_JMM: MetaSpace 元空间的详解

JVM_JMM: MetaSpace 元空间的详解

作者: CalmHeart | 来源:发表于2019-07-09 21:46 被阅读0次

Meta Space是JDK1.8引入的,在JDK1.8使用的是方法区,永久代(Permnament Generation)。
元空间存储的是元信息,使用的是操作系统的本地内存,可以是不连续的,由元空间虚拟机进行管理。可以产生OutOfMemoryError


方法区产生内存溢出的错误 需要调整内存参数,采用特殊的处理手段 初始的大小是21M,元空间虚拟机进行GC,内存不足可以进行内存扩展,可以扩展到 物理内存的最大存储大小 1. 显示设置元空间的大小,不会自动扩展 2. 元空间存储的数据是什么?存放一些元信息(Class元信息 动态生成字节码编译时不存在运行时生成 ,可以使用CGLIB、JDK动态代理、JSP等可以操作),不存放对象实例等数据 。


引入CGLIB:

 <!--引入cglib演示元空间的内存溢出-->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.2.12</version>
    </dependency>

在启动的时候设置元空间的大小:

-XX:MaxMetaspaceSize=200m


源文件:

//-XX:MaxMetaspaceSize=10m
public class MyTest5 {
  public static void main(String[] args) {
    for (; ; ) {
      Enhancer enhancer = new Enhancer();
      enhancer.setSuperclass(MyTest5.class);
      enhancer.setUseCache(false);
      enhancer.setCallback((MethodInterceptor) (object, method, args1, proxy) ->
          proxy.invokeSuper(object, args1)
      );
      System.out.println("creating...");
      enhancer.create();
    }
  }
}

运行结果:

creating...
creating...
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:348)
    at net.sf.cglib.proxy.Enhancer.generate(Enhancer.java:492)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:117)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:294)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:305)
    at com.compass.spring_lecture.memory.MyTest5.main(MyTest5.java:24)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:459)
    at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:339)
    ... 6 more
Caused by: java.lang.OutOfMemoryError: Metaspace
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    ... 11 more

使用jconsole监测:


监测结果

显示类的数量在持续地攀升。

在jconsole上勾选,详细输出:


勾选详细输出

Console输出结果:

creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12901 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12902 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12903 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12904 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12905 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12906 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12907 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_12908 from file:/C:/spring_lecture/target/classes/]
creating...

修改虚拟机参数,如下:
-XX:MaxMetaspaceSize=200m -XX:+TraceClassLoading
输出结果:

creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_7405 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_7406 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_7407 from file:/C:/spring_lecture/target/classes/]
creating...
[Loaded com.compass.spring_lecture.memory.MyTest5$$EnhancerByCGLIB$$20cc5f05_7408 from file:/C:/spring_lecture/target/classes/]

使用jvisualvm观察结果如下:


image.png

参考文章:
Java 永久代去哪儿了

相关文章

网友评论

      本文标题:JVM_JMM: MetaSpace 元空间的详解

      本文链接:https://www.haomeiwen.com/subject/kcdvhctx.html