当前位置:网站首页>If you encounter oom online, how to solve it?
If you encounter oom online, how to solve it?
2022-07-27 05:46:00 【nuzzzzz】

OOM It means that there are loopholes in the program , It could be code or JVM Caused by parameter configuration . This article talks to readers ,Java Process triggered OOM How to check after
It is often said to maintain awe of the production environment , Quick problem solving is also a sign of awe
why OOM?
OOM Full name “Out Of Memory”, Indicates that memory is exhausted . When JVM Because there is not enough memory to allocate space for objects , And when the garbage collector has no space to recycle , Will throw this error
Why does it show up OOM, Generally caused by these problems
Too little allocation :JVM Small initialization memory , Business uses a lot of memory ; Or different JVM Area allocation memory is unreasonable
Code vulnerability : An object is frequently applied for , It's not used, but it's not released , Cause memory to run out
Memory leak : The used memory has not been released , Cause the virtual machine can't use the memory again , At this point, the memory leaks . Because applicants don't have to , And can't be assigned to others by virtual machine
out of memory : The requested memory exceeds JVM The memory size that can be provided , This is called overflow
Memory leaks persist , Finally, it will overflow , The two are causal
common OOM
More common OOM There are several types of
java.lang.OutOfMemoryError: PermGen space
Java7 Forever ( Method area ) overflow , It is used to store class information that has been loaded by the virtual machine 、 Constant 、 Static variables 、 Real time compiler compiled code and other data . Whenever a class is first loaded , Metadata will be stored in the permanent generation
It usually occurs in a large number of Class Object or JSP page , Or use CgLib Dynamic agent technology leads to
We can go through -XX:PermSize and -XX:MaxPermSize Modify the size of the method area
Java8 Change permanent generation to meta space , Report errors : java.lang.OutOfMemoryError: Metadata space, The meta space memory is insufficient. Dynamic expansion is performed by default
java.lang.StackOverflowError
Virtual machine stack overflow , Generally, it is due to the existence of Dead loop or deep recursive call Caused by the . If the stack size is set too small, overflow will also occur , Can pass -Xss Set the stack size
The virtual machine throws a stack overflow error , You can locate the wrong class in the log 、 Method
java.lang.OutOfMemoryError: Java heap space
Java Heap memory overflow , The cause of overflow is generally due to JVM Memory leaks or improper heap settings
If it's a memory leak , You can view the leak object through the tool GC Roots Reference chain of . Master the type information of the leakage object and GC Roots Reference chain information , You can accurately locate the location of the leaked code
If there is no memory leak , It's that objects in memory really have to live , Then you should check the heap parameters of the virtual machine (-Xmx And -Xms), See if you can increase the memory of the virtual machine
Summary : Overflow scenarios of method area and virtual machine stack are not discussed too much in this article , The following mainly explains the common Java Pile space OOM Investigation thought
see JVM Distribution of memory
Suppose we Java application PID by 15162, Enter command view JVM Distribution of memory jmap -heap 15162
[[email protected] ~]# jmap -heap 15162 Attaching to process ID 15162, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 using thread-local object allocation. Mark Sweep Compact GC Heap Configuration: MinHeapFreeRatio = 40 # Minimum heap usage ratio MaxHeapFreeRatio = 70 # Maximum heap usable ratio MaxHeapSize = 482344960 (460.0MB) # Maximum heap space size NewSize = 10485760 (10.0MB) # New generation allocation size MaxNewSize = 160759808 (153.3125MB) # Maximum Cenozoic allocable size OldSize = 20971520 (20.0MB) # Old age size NewRatio = 2 # Cenozoic proportion SurvivorRatio = 8 # The new generation and Survivor The proportion MetaspaceSize = 21807104 (20.796875MB) # Metaspace size CompressedClassSpaceSize = 1073741824 (1024.0MB) # Compressed Class Space Space size limit MaxMetaspaceSize = 17592186044415 MB # Maximum Metaspace size G1HeapRegionSize = 0 (0.0MB) # G1 Single Region size Heap Usage: # Heap usage New Generation (Eden + 1 Survivor Space): # The new generation capacity = 9502720 (9.0625MB) # The total capacity of the new generation used = 4995320 (4.763908386230469MB) # The new generation has used free = 4507400 (4.298591613769531MB) # Cenozoic residual capacity 52.56726495150862% used # Proportion of new generation use Eden Space: capacity = 8454144 (8.0625MB) # Eden Total area capacity used = 4029752 (3.8430709838867188MB) # Eden Zone used free = 4424392 (4.219429016113281MB) # Eden Area remaining capacity 47.665996699370154% used # Eden The proportion of district use is From Space: # One of them Survivor Area memory distribution capacity = 1048576 (1.0MB) used = 965568 (0.92083740234375MB) free = 83008 (0.07916259765625MB) 92.083740234375% used To Space: # the other one Survivor Area memory distribution capacity = 1048576 (1.0MB) used = 0 (0.0MB) free = 1048576 (1.0MB) 0.0% used tenured generation: # Old age capacity = 20971520 (20.0MB) used = 10611384 (10.119804382324219MB) free = 10360136 (9.880195617675781MB) 50.599021911621094% used 10730 interned Strings occupying 906232 bytes.
By looking at JVM Memory allocation and runtime usage , You can judge whether the memory allocation is reasonable
in addition , Can be in JVM View the most resource consuming objects at runtime ,jmap -histo:live 15162 | more
JVM The list of memory objects is sorted according to the memory occupied by the objects
instances: Number of instances
bytes: Company byte
class name: Class name

Obviously see CustomObjTest Object instance and too much memory
It is a pity , The scheme has limitations , Because it can only check the problem that the object occupies too much memory
among “[” Representative array , for example “[C” representative Char Array ,"[B" representative Byte Array . If the array takes up too much memory , We don't know which objects hold it , So we need Dump Memory for offline analysis
jmap -histo:live Execute this command ,JVM Will trigger GC, Then statistics
Dump File analysis
Dump File is Java Memory image of process , Which mainly includes system information 、 Virtual machine properties 、 Full thread Dump、 Status of all classes and objects Etc
When the program has memory overflow or GC In case of abnormal situation , doubt JVM It happened. Memory leak , Then we can export Dump File analysis
JVM Start the parameter configuration and add the following parameters
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./( Parameter is Dump File generation path )
When JVM happen OOM Exceptions are automatically exported Dump file , The default format of the file name is :java_pid{pid}.hprof
The above configuration is thrown in the application OOM Automatically export after Dump, Or you can JVM Run time export Dump file
jmap -dump:file=[ File path ] [pid] # Example jmap -dump:file=./jvmdump.hprof 15162
Write a test code locally , To validate the OOM And analysis Dump file
Set up VM Parameters :-Xms3m -Xmx3m -XX:+ HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./
public static void main(String[] args) { List<Object> oomList = Lists.newArrayList(); // Create objects in an infinite loop while (true) { oomList.add(new Object()); } }
Learn from the error information ,java heap space Express OOM Occurs in the heap area , And created hprof Binary files are in the current folder

JvisualVM analysis
Dump There are many analysis tools , Relatively speaking JvisualVM、JProfiler、Eclipse Mat, Use more people . Let's say JvisualVM Give an example of Dump file

List two common functions , The first is to see the trigger OOM The thread stack of , Clearly know the cause of program overflow

The second is to view JVM Keep the largest object in memory , You can freely choose the number of troubleshooting

Clicking an object can also jump to the specific object reference details page

In this paper, Dump The file is relatively simple , There are a variety of reasons for mistakes in the formal environment , So it's not right to Dump Deep parsing of files
Be careful :JvisualVM If the analysis is large Dump file , You may not be able to open it because of insufficient memory , Need to adjust the default memory
Summary and review
If you encounter JVM out of memory , You can check in the following steps
jmap -heap Check whether the memory allocation is too small
jmap -histo Check to see if there is an obvious over allocation of objects and no release
jmap -dump export JVM Current memory snapshot , Use JDK Self contained or MAT And other tools to analyze snapshots
If the problem cannot be located above , Then you need to check whether the application is constantly creating resources , Such as network connection or thread , May lead to the depletion of system resources
边栏推荐
- Personal collection code cannot be used for business collection
- What are alpha and beta tests?
- Configure the route and go to the login home page to send the request
- jenkins构建镜像自动化部署
- PHP 实现与MySQL的数据交互
- 建设创客教育运动中的完整体系
- Specific matters of opening accounts of futures companies
- 亚马逊测评自养号,如何进行系统性的学习?
- How to judge whether an object is empty in JS
- 「PHP基础知识」使用echo语句输出信息
猜你喜欢

期货开户要和客户经理详谈政策

Maintain login and route jump

The written test questions of 25 large Internet companies are summarized, and I have encountered packages.

How to open a general commodity futures account

怎么开立普通商品期货账户

期货开户公司交返怎么申请?

使用Docker部署Redis进行高可用主从复制

Sealem Finance - a new decentralized financial platform based on Web3

Mysql5.7版本如何实现主从同步

如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分
随机推荐
[MRCTF2020]PYWebsite 1
神芷迦蓝寺
期货公司的评级和查询详情
Edit delete user
攻防世界-mfw
Dimitra 和 Ocean Protocol 解读农业数据背后的秘密
攻防世界-lottery
一文读懂Elephant Swap的LaaS方案的优势之处
Build a complete system in the maker education movement
刷脸商业逻辑重大改变商户争抢烽烟再起
GameFi如何破圈,AQUANEE靠真正“P2E”展现风采
The difference between for... Of and for... In JS
[CISCN2019 华东南赛区]Web11 1
Sealem Finance-基于Web3的全新去中心化金融平台
Department management of cloud e-office projects
dirsearch[目录扫描工具]
kettle如何处理文本数据传输为‘‘而不是null
我的大四
刷脸支付永远不会过时只会不断的变革
MOVE PROTOCOL推出测试版,更可“0撸”参与P2E