美文网首页
写个 Java 程序让 Metaspace 溢出

写个 Java 程序让 Metaspace 溢出

作者: jyjz2008 | 来源:发表于2018-11-24 21:37 被阅读0次

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

    相关文章

      网友评论

          本文标题:写个 Java 程序让 Metaspace 溢出

          本文链接:https://www.haomeiwen.com/subject/qdtuqqtx.html