写个 Java 程序让 Metaspace 溢出

Maven 依赖


    cglib
    cglib
    3.1

虚拟机参数

-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();
        }
    }
}

你可能感兴趣的:(写个 Java 程序让 Metaspace 溢出)