java 元数据区_Java元数据区域(MetaSpace)OOM

本文通过一个测试案例展示了如何触发Java虚拟机(JVM)的元数据区内存溢出错误。通过不断创建类并禁用缓存,最终导致元数据区耗尽内存,引发OutOfMemoryError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

元数据区

元数据区取代了1.7版本及以前的永久代。元数据区和永久代本质上都是方法区的实现。方法区存放虚拟机加载的类信息,静态变量,常量等数据。

JVM配置

-XX:MetaspaceSize=10M

-XX:MaxMetaspaceSize=10M

-XX:+HeapDumpOnOutOfMemoryError

测试代码

public classMetaSpaceOOM {public static voidmain(String[] args) {while (true) {

Enhancer enhancer=newEnhancer();

enhancer.setSuperclass(OOMObject.class);

enhancer.setUseCache(false);

enhancer.setCallback(newMethodInterceptor() {

@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throwsThrowable {returnmethodProxy.invokeSuper(o,args);

}

});

enhancer.create();

}

}static classOOMObject {

}

}

运行结果

Dumping heap to java_pid2776.hprof ...

Heap dump file created [4179143 bytes in 0.023secs]

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.00secs]

[GC (Allocation Failure) [PSYoungGen: 67823K->3272K(75264K)] 67839K->3296K(247296K), 0.0049203 secs] [Times: user=0.00 sys=0.00, real=0.00secs]

[GC (Metadata GC Threshold) [PSYoungGen: 39236K->3816K(75264K)] 39260K->3848K(247296K), 0.0040988 secs] [Times: user=0.00 sys=0.00, real=0.00secs]

[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.02secs]

[GC (Last ditch collection) [PSYoungGen: 0K->0K(102400K)] 3694K->3694K(201216K), 0.0006640 secs] [Times: user=0.00 sys=0.00, real=0.00secs]

[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.05secs]

java.lang.OutOfMemoryError: Metaspace

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值