Meta Space是JDK1.8引入的鸽嫂,在JDK1.8使用的是方法區(qū),永久代(Permnament Generation)髓窜。
元空間存儲的是元信息扇苞,使用的是操作系統(tǒng)的本地內(nèi)存,可以是不連續(xù)的寄纵,由元空間虛擬機進行管理鳖敷。可以產(chǎn)生OutOfMemoryError
方法區(qū)產(chǎn)生內(nèi)存溢出的錯誤 需要調整內(nèi)存參數(shù),采用特殊的處理手段 初始的大小是21M,元空間虛擬機進行GC,內(nèi)存不足可以進行內(nèi)存擴展,可以擴展到 物理內(nèi)存的最大存儲大小 1. 顯示設置元空間的大小,不會自動擴展 2. 元空間存儲的數(shù)據(jù)是什么?存放一些元信息(Class元信息 動態(tài)生成字節(jié)碼編譯時不存在運行時生成 ,可以使用CGLIB程拭、JDK動態(tài)代理定踱、JSP等可以操作),不存放對象實例等數(shù)據(jù) 。
引入CGLIB:
<!--引入cglib演示元空間的內(nèi)存溢出-->
<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監(jiān)測:
監(jiān)測結果
顯示類的數(shù)量在持續(xù)地攀升恃鞋。
在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...
修改虛擬機參數(shù),如下:
-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 永久代去哪兒了