【深入浅出-JVM】(9): 方法区

概念

方法区是虚拟机规范定义的,是所有线程共享的内存区域,保存系统的类的信息。比如:类的字段、方法、常量池、构造函数的字节码内容、代码、JIT 代码

永久代、metaspace 是对方法区的实现。

Hotspot 实行分代管理内存(新生代、老年代、永久代)

jdk8 实现方法区用 metaspace 堆外内存

方法区溢出

虚拟机参数:

jdk1.7

-XX:PermSize=5M -XX:MaxPermSize=10M

jdk1.8

-XX:MaxMetaspaceSize=150M

代码


package com.mousycoder.mycode.thinking_in_jvm;



import net.sf.cglib.proxy.Enhancer;

import net.sf.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.MethodProxy;



import java.lang.reflect.Method;



/**

 * 方法区溢出

 * 

 * @version 1.0

 * @author: mousycoder

 * @date: 2019-06-22 15:44

 */

public class PermgenOOM {



    public static void main(String[] args) throws InterruptedException {

        int i = 0;

        while (true) {

            Enhancer enhancer = new Enhancer();

            enhancer.setSuperclass(User.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,objects);

                }

            });

            enhancer.create();

            Thread.sleep(1000);

        }





    }

}



 class User {

    private long id;



    private String name;



}



jdk1.7

jdk1.8


感谢您的耐心阅读,如果您发现文章中有一些没表述清楚的,或者是不对的地方,请给我留言,您的鼓励是作者写作最大的动力。

作 者 : @mousycoder

原文出处 : http://mousycoder.com/thinking-in-jvm/9/

posted @ 2019-07-17 18:13  mousycoder  阅读(260)  评论(0编辑  收藏  举报