JVM(四)-- JVM内存模型

整体描述

前面对运行时数据区描述了很多,其实重点存储数据的是堆和方法区(非堆),所以内存的设计也着重从

这两方面展开(注意这两块区域都是线程共享的)。对于虚拟机栈,本地方法栈,程序计数器都是线程私有的。

(1)一块是非堆区,一块是堆区
(2)堆区分为两大块:一个是Old区,一个是Young区
(3)Young区分为两大块:一个是Survivor区(S0+S1),一块是Eden区
(4)S0和S1一样大,也可以叫From和To

Java Memory Model (JMM待完善)

官网:https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4

对象在内存中的分配回收

代码示例

-Xmx50M -Xms50M

堆的OOM
@RestController
public class HeapController {
    List<Worker list=new ArrayList<Worker();
    @GetMapping("/heap")
    public String heap() throws Exception{
        while(true){
            list.add(new Worker());
        }
    }
}

运行结果:

方法区的OOM

-XX:MetaspaceSize=50M -XX:MaxMetaspaceSize=50M

(1)asm依赖

<dependency   
    <groupIdasm</groupId   
    <artifactIdasm</artifactId  
    <version3.3.1</version
</dependency

(2)工具类

public class MetaspaceUtil extends ClassLoader {

    public static List<Class<? createClasses() {
        List<Class<? classes = new ArrayList<Class<?();
        for (int i = 0; i < 10000000; ++i) {
            ClassWriter cw = new ClassWriter(0);
            cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null,
                    "java/lang/Object", null);
            MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init",
                    "()V", null, null);
            mw.visitVarInsn(Opcodes.ALOAD, 0);
            mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object",
                    "<init", "()V");
            mw.visitInsn(Opcodes.RETURN);
            mw.visitMaxs(1, 1);
            mw.visitEnd();
            MetaspaceUtil test = new MetaspaceUtil();
            byte[] code = cw.toByteArray();
            Class<? exampleClass = test.defineClass("Class" + i, code, 0, code.length);
            classes.add(exampleClass);
        }
        return classes;
    }
}

(3)代码

@RestController
public class NonHeapController {

    List<Class<? list=new ArrayList<Class<?();
    @GetMapping("/nonheap")
    public String heap(){
        while(true){
            list.addAll(MetaspaceUtil.createClasses());
        }
    }
}

运行结果:

栈溢出
public class StackOverFlowDemo {

    public static long count=0;

    public static void method(){
        System.out.println(count++);
        method();
    }

    public static void main(String[] args) {
        method();
    }
}

运行结果:

posted @ 2022-12-08 17:42  snail灬  阅读(39)  评论(0编辑  收藏  举报