写个 Java 程序让 Metaspace 溢出

2018-11-24  本文已影响0人  jyjz2008

Maven 依赖

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.1</version>
</dependency>

虚拟机参数

-XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M -verbose:class

Java 程序

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class Permanent {
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            System.out.println(i++);
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Permanent.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();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读