当前位置:网站首页>13 and OOM simulation

13 and OOM simulation

2022-08-03 16:08:00 jerry_dyy

通过上文的分析,我们知道:Only possible in the method area(元数据区)and old age may occurOOM,So let's simulate both happening hereOOM的场景.

元数据区发生OOM:

这里我们通过cglibTo continuously create dynamically generated classes to simulate what happens in the metadata areaOOM的场景.

So called dynamically generated classes,That is, the classes are not written by hand.The classes we hand out are all.java,然后会编译为.class,Then it will be loaded into the metadata area by the class loader to become a statically generated class.The dynamically generated classes are in the process of running the system,通过操作、A class that edits the bytecode to generate dynamically.

启动JVMWhen setting the metadata area size:

-XX:MetaspaceSize=10m

-XX:MaxMetaspaceSize=10m

Then write the following code:

public class MetaspaceOOMDemo {

    public static void main(String[] args) {
        int count = 0;
        while (true){
            System.out.println("count: " + ++count);
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Car.class);//To generate one dynamicallyCar类的子类
            enhancer.setUseCache(false);//Whether to reuse dynamic classes
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                    if(method.getName().equals("run")){
                        System.out.println("Safety checks before the car starts");
                        return proxy.invokeSuper(obj,args);//Specifically to callCarclass method
                    }else {
                        return proxy.invokeSuper(obj,args);
                    }
                }
            });
            Car car = (Car) enhancer.create();//这里通过enhancer生成的car就是一个继承CarObject generated by the dynamic class of the class
            car.run();
        }
    }
}

class Car{
    public void run(){
        System.out.println("汽车启动,开始行使...");
    }
}

Because of the settings in the above codeenhancer.setUseCache(false),So new dynamic classes will be created constantly,These dynamic classes will continue to enter the metadata area,Eventually lead to the metadata areaOOM.

启动,Find out and run to the end246次的时候,发生:java.lang.OutOfMemoryError: Metaspace.Here you can see what happens in the metadata areaOOM,And the program exits directly.

老年代发生OOM:

We simulate the occurrence of the old generation by means of memory leaksOOM.

设置JVM启动参数,Set heap memory to 1MB大小:

-Xms1m –Xmx1m

编写以下代码:

public class HeapOOMDemo {

    public static void main(String[] args) {
        long count = 0;
        List<Object> list = new ArrayList<>();
        while (true){
            list.add(new Object());
            System.out.println("count: " + ++count);
        }
    }
}

The above code will keep creatingObject对象,and will not be released,Eventually it will be a memory leak problem,lead to heap memoryOOM.

启动,跑到第14053次的时候,发生:java.lang.OutOfMemoryError: Java heap space.堆内存溢出,And the program exits directly.

1、JVM是如何工作的?_jerry_dyy的博客-CSDN博客_jvm是如何运行的

2、JVM的类加载机制_jerry_dyy的博客-CSDN博客

3、JVM内存区域划分_jerry_dyy的博客-CSDN博客_jvm的内存区域划分

4、JVM垃圾回收机制_jerry_dyy的博客-CSDN博客

5、JVM分代模型--新生代 的垃圾回收_jerry_dyy的博客-CSDN博客_jvm新生代划分

6、JVM分代模型--老年代 的垃圾回收_jerry_dyy的博客-CSDN博客

7、常见的垃圾回收器_jerry_dyy的博客-CSDN博客

8、JVM优化简介_jerry_dyy的博客-CSDN博客

9、学会查看GC日志_jerry_dyy的博客-CSDN博客

10、摸清JVM运行状况_jerry_dyy的博客-CSDN博客

11、摸清JVM对象分布_jerry_dyy的博客-CSDN博客

12、OOM简介_jerry_dyy的博客-CSDN博客

13、OOM模拟_jerry_dyy的博客-CSDN博客

原网站

版权声明
本文为[jerry_dyy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/215/202208031546409788.html