元数据区
元数据区取代了1.7版本及以前的永久代。元数据区和永久代本质上都是方法区的实现。方法区存放虚拟机加载的类信息,静态变量,常量等数据。
JVM配置
-XX:MetaspaceSize=10M
-XX:MaxMetaspaceSize=10M
-XX:+HeapDumpOnOutOfMemoryError
测试代码
public class MetaSpaceOOM { public static void main(String[] args) { while (true) { Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(OOMObject.class); enhancer.setUseCache(false); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { return methodProxy.invokeSuper(o,args); } }); enhancer.create(); } } static class OOMObject { } }
运行结果
Dumping heap to java_pid2776.hprof ... Heap dump file created [4179143 bytes in 0.023 secs] Exception in thread "main" java.lang.OutOfMemoryError: Metaspace at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:538) at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:582) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:131) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:569) at org.springframework.cglib.proxy.Enhancer.create(Enhancer.java:384) at com.example.oom.MetaSpaceOOM.main(MetaSpaceOOM.java:21)
GC LOG
[GC (Allocation Failure) [PSYoungGen: 64512K->3311K(75264K)] 64512K->3327K(247296K), 0.0056889 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC (Allocation Failure) [PSYoungGen: 67823K->3272K(75264K)] 67839K->3296K(247296K), 0.0049203 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC (Metadata GC Threshold) [PSYoungGen: 39236K->3816K(75264K)] 39260K->3848K(247296K), 0.0040988 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Metadata GC Threshold) [PSYoungGen: 3816K->0K(75264K)] [ParOldGen: 32K->3694K(98816K)] 3848K->3694K(174080K), [Metaspace: 9913K->9913K(1058816K)], 0.0195453 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] [GC (Last ditch collection) [PSYoungGen: 0K->0K(102400K)] 3694K->3694K(201216K), 0.0006640 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Last ditch collection) [PSYoungGen: 0K->0K(102400K)] [ParOldGen: 3694K->2157K(188928K)] 3694K->2157K(291328K), [Metaspace: 9913K->9913K(1058816K)], 0.0355431 secs] [Times: user=0.14 sys=0.02, real=0.05 secs] java.lang.OutOfMemoryError: Metaspace