当前位置:网站首页>The difference between JVM memory overflow and memory leak
The difference between JVM memory overflow and memory leak
2022-07-26 12:03:00 【pan_ junbiao】
1、 Distinction of concepts
1.1、 Memory leak (memory leak)
After program running , There is no release Memory space occupied .
A memory leak It doesn't seem to have a big impact , But memory leaks Keep accumulating , Eventually, the available memory will become less and less .

for instance , The total memory size is 100 MB, Yes 40MB Of memory has been unable to recycle , Then only 60MB . this 40MB Memory leak .
Memory leak , After the program runs , No free memory .
1.2、 out of memory (out of memory)
Program runtime , When applying for memory space , There is not enough memory space for its normal use , The program stops , And throw out of memory .
For example, when the program is running, it applies for a 10MB Space , But the current available memory is only 5MB, The program doesn't work properly , This is memory overflow .
out of memory , It can be understood that the memory required for program operation is greater than the currently available memory .
1.3 give an example
(1) In singleton mode , The lifecycle of the singleton is as long as the application , So if a singleton program holds a reference to an external object , Then this external object cannot be recycled , Will lead to Memory leak The birth of .
(2) Some offer close Due to the unclosed resources of Memory leak . Database connection (dataSource.getConnection() ), network connections (socket) and IO The connection of the stream must be in finally in close, Otherwise it can't be recycled .
(3) Read large files , The file read at one time is larger than the available memory , It can lead to out of memory . The available memory is 1G, How to read 2G What about the documents ? To build a 100MB Byte array of , read 10 Time .
2、 The difference and connection between the two
2.1 difference
Memory leak : After program running , The occupied memory is not fully released .
out of memory : Program runtime , The memory required is larger than the currently available memory , Out of memory , Program cannot continue , Throw out “ out of memory ”, The program is interrupted , end .
2.2 contact
once Memory leak It may have no obvious impact on the operation of the program , many times Memory leak Will eventually lead to out of memory .
For example, the total memory size is 100MB, There are , Yes 10MB There is no release , At present Available memory also 90MB, The program can also run . But after many runs , Available memory Ultimately for 0, When there is no memory available or memory is insufficient , The next time the program runs , Because of insufficient memory , And it appears out of memory .
3、 The causes and solutions of memory overflow
3.1 Cause of memory overflow
There are many reasons for memory overflow , Xiaobian lists the following common :
- The amount of data loaded in memory is too large , Such as fetching too much data from the database at one time ;
- There are references to objects in collection classes , Not empty after use , bring JVM Can't recycle ;
- There are dead loops in the code or loops that produce too many duplicate object entities ;
- In the third-party software used BUG;
- The memory value of startup parameter is set too small .
3.2 Memory overflow solution
First step , modify JVM Launch parameters , Add memory directly .(-Xms、-Xmx Parameters must not forget to add )
The second step , Check the error log , see “OutOfMemory” Whether there are other anomalies or errors before the error .
The third step , Walk through and analyze the code , Find out where a memory overflow might occur .
Focus on the following points :
1. Check the database query , Is there a query to get all the data .
Generally speaking , If you take 100000 records to memory at a time , May cause memory overflow .
This problem is more hidden , Before going online , There is less data in the database , It's not easy to have problems , After the launch ,
There's a lot of data in the database , A query may cause memory overflow . Therefore, the database query should be paged as much as possible .
2. Check the code for dead loops or recursive calls .
3. Check if there is a large loop that repeatedly generates new object entities .
4. Check the database query , Is there a query to get all the data .
Generally speaking , If you take 100000 records to memory at a time , May cause memory overflow .
This problem is more hidden , Before going online , There is less data in the database , It's not easy to have problems ,
After the launch , There's a lot of data in the database , A query may cause memory overflow . Therefore, the database query should be paged as much as possible .
5. Check List、MAP Wait until the collection object is used , Problems not cleared .
List、MAP The collection object will always have a reference to the object , So that these objects cannot be GC Recycling .Step four , Use the memory view tool to dynamically view memory usage .
Original author :Sharley
Original address :https://www.cnblogs.com/Sharley/p/5285045.html
边栏推荐
- 什么是OOM,为什么会OOM及一些解决方法
- MILA旗下初创企业Ubenwa获得250万美元投资,研究婴儿健康AI诊断
- Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
- Introduction to FPGA (II) - one out of two selector
- pytest接口自动化测试框架 | pytest之fixture介绍
- Server memory failure prediction can actually do this!
- CVPR 2022 new SOTA for monocular depth estimation new CRFs: neural window fullyconnected CRFs
- Recalling Sister Feng
- Redis实现Single单点登入--系统框架搭建(一)
- Talking about web vitals
猜你喜欢

Miccai2022 paper | evolutionary multi-objective architecture search framework: application in covid-19 3D CT classification

Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)

Understand the string class

基于STM32的SIM900A发送中文和英文短信

Some practical, commonly used and increasingly efficient kubernetes aliases

11 "pocket" universities in China! Running on campus and leaving the school before accelerating

一些常用的文章写作使用方法和技巧

GA-RPN:引导锚点的建议区域网络

Ga-rpn: recommended area network for guiding anchors

专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
随机推荐
Cohere博客:在生产环境中运行大型语言模型-推理框架概览
pytest接口自动化测试框架 | pytest之fixture介绍
Several inventory terms often used in communication
el-form 每行显示两列,底部按钮居中
3.1 create menu and game page - up
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
pytest接口自动化测试框架 | 使用装饰器修饰需要运行的用例
Some practical, commonly used and increasingly efficient kubernetes aliases
Pytoch deep learning quick start tutorial -- mound tutorial notes (I)
V01 - XX,记录美好生活从日志开始
Flutter JNI confusion introduction.So file release package flash back
Network protocol: tcp/ip protocol
pytest接口自动化测试框架 | pytest配置文件
Flink cdc 是不是只支持 sql-client的方式提交SQL脚本啊
Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time
MICCAI2022论文 | 进化多目标架构搜索框架:在COVID-19三维CT分类中的应用
常用的 list.isEmpty() 为何突然报null?
[Anhui University] information sharing of postgraduate entrance examination and re examination
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
pytest接口自动化测试框架 | pytest获取执行数据、pytest禁用插件