当前位置:网站首页>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博客
9、学会查看GC日志_jerry_dyy的博客-CSDN博客
10、摸清JVM运行状况_jerry_dyy的博客-CSDN博客
边栏推荐
猜你喜欢
【Unity入门计划】制作RubyAdventure01-玩家的创建&移动
全新探险者以40万的产品击穿豪华SUV价格壁垒
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
高压直流输电(HVDC)的最优潮流(OPF)(Matlab代码实现)
[微信小程序开发者工具] × #initialize
Daily practice------There are 10 numbers that are required to be output from large to small by selection method
spark入门学习-1
深入浅出Flask PIN
Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology
【码蹄集新手村600题】将一个函数定义宏
随机推荐
Difference and performance comparison between HAL and LL library of STM32
Interpretation of the 2021 Cost of Data Breach Report
Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology
一文看懂推荐系统:召回03:基于用户的协同过滤(UserCF),要计算用户之间的相似度
【码蹄集新手村600题】将一个函数定义宏
I am doing open source in Didi
深入浅出Flask PIN
49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
Internship Road: Documenting Confusion in My First Internship Project
【899. Ordered Queue】
NodeJs - cross domain
How to get the 2 d space prior to ViT?UMA & Hong Kong institute of technology & ali SP - ViT, study for visual Transformer 2 d space prior knowledge!.
After the cnpm installation is successful, the prompt is not an internal and external command, nor is it a runnable command solution
为什么我强烈推荐使用智能化async?
新一代网状网协议T-Mesh无线通信技术优势介绍
ModelWhale 云端运行 WRF 中尺度数值气象模式,随时随地即开即用的一体化工作流
How to play deep paging with hundreds of millions of data?Compatible with MySQL + ES + MongoDB
"Avnet Embedded Weekly" Issue 276: 2022.07.25--2022.07.31
Leetcode76. Minimal Covering Substring
6000 字+,帮你搞懂互联网架构演变历程!