zoukankan      html  css  js  c++  java
  • 【深入浅出-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/

    我是浩哥,希望我的思考能帮到您!
  • 相关阅读:
    HTML转PDF
    观察者模式分析
    异常解决方案记录
    复杂结构数据提交
    在Spring Controller中将数据缓存到session
    Eclipse
    redis数据类型-散列类型
    redis数据类型-字符串类型
    redis键操作
    redis性能优化
  • 原文地址:https://www.cnblogs.com/mousycoder/p/11202785.html
Copyright © 2011-2022 走看看