当前位置:网站首页>[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
2022-07-02 19:00:00 【Half old 518】
front said
Author's brief introduction : Half old 518, Long distance runner , Determined to persist in writing 10 Blog of the year , Focus on java Back end
Column Introduction : Case driven introduction JVM knowledge , Teach you how to use JVM Troubleshooting 、 Evaluation code 、 Optimize performance
The article brief introduction : Introduce the related concepts of heap 、 Teach you to check 5 A common JVM Pile of cases
List of articles
6. Pile up
6.1 Characteristics of reactor
Use new All objects created by keyword will use heap .
characteristic :
- Thread sharing , The objects in the heap need to consider thread safety .
- There's a garbage collection mechanism .
6.2 Heap memory overflow problem
There is a garbage collection mechanism in the heap , But the premise of garbage collection is that the objects in the heap are no longer referenced ( actually , The algorithm for recycling references is the root reachable Algorithm , I'll talk about it later , The statement here is inaccurate ), So if we have too many objects that cannot be recycled , It may cause memory overflow .
public class MemoryOverFlow {
public static void main(String[] args) {
int i = 0;
String a = "hello";
List list = new ArrayList(); // until catch Code block execution , Has been used
try {
while (true) {
list.add(a);
a = a + a;
i++;
}
} catch (Throwable e) {
// Use Throwable, If you use Exception I can't wrap it up Error,i Can't be printed out
e.printStackTrace();
System.out.println(i);
}
}
}
appear OutOfMemoryError
java.lang.OutOfMemoryError: Overflow: String length out of range
at java.base/java.lang.StringConcatHelper.checkOverflow(StringConcatHelper.java:57)
at java.base/java.lang.StringConcatHelper.mix(StringConcatHelper.java:138)
at java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:420)
at MemoryOverFlow.main(MemoryOverFlow.java:12)
28
another : Parameters -Xmx You can set jvm Memory size , When troubleshooting heap memory problems, you can set it to be small ( Such as 8m), More likely to expose memory overflow problems . Setup method : Click on build and run Go with you modify options->add vm options.
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-2ry02UEf-1656678910763)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630210233084.png)]](/img/54/8a18cd30e6186528599c0556b1ee3b.png)
6.3 Evaluation of the impact of code memory performance
I wrote a piece of code in my work , How to judge the impact of a piece of code on memory performance ? You can use the following tools .
jpsSee what the system has java processjmapCheck the occupation of heap memory at a certain timejconsoleMultifunctional real-time monitoring tool
Through the following demo To demonstrate .
public class jvmdemo {
public static void main(String[] args) throws InterruptedException {
System.out.println("1....."); // Output hint , Easy to carry out Heap Dump
Thread.sleep(60000); // to 30s Time is used for Heap Dump
byte [] arr = new byte[1024 * 1024 * 10];
System.out.println("2.......");
Thread.sleep(60000);
arr = null;
System.gc();
System.out.println("3......");
Thread.sleep(100000L);
}
}
When the output 1… after , Execute first jps see jvmdemo Corresponding pid
tip: If you are windows System ,jps No results returned , You can refer to the blog
give the result as follows .

perform jmap -heap xxx(pid) Check the heap memory usage at this time .
tip:
If you execute jmap Report errors .
Error: -heap option used Cannot connect to core dump or remote debug server. Use jhsdb jmap insteadBecause jdk8 Later versions, previous
jmap -heap xxx(pid)The command can no longer be used . You can use the command insteadjhsdb jmap --heap --pid xxx.
In the prompt message output 1,2,3 After three operations, the results are as follows .
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 0 (0.0MB)
free = 8531214336 (8136.0MB)
0.0% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 29360128 (28.0MB)
used = 0 (0.0MB)
free = 29360128 (28.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 0
capacity = 507510784 (484.0MB)
used = 0 (0.0MB)
free = 507510784 (484.0MB)
0.0% used
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 12582912 (12.0MB)
free = 8518631424 (8124.0MB)
0.14749262536873156% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 29360128 (28.0MB)
used = 0 (0.0MB)
free = 29360128 (28.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 3
capacity = 507510784 (484.0MB)
used = 12582912 (12.0MB)
free = 494927872 (472.0MB)
2.479338842975207% used
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 673872 (0.6426544189453125MB)
free = 8530540464 (8135.357345581055MB)
0.007898898954588403% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 8388608 (8.0MB)
used = 0 (0.0MB)
free = 8388608 (8.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 1
capacity = 8388608 (8.0MB)
used = 673872 (0.6426544189453125MB)
free = 7714736 (7.3573455810546875MB)
8.033180236816406% used
Focus on uesd This one , You can see the changing process of memory in the code , here JVM version yes 16.0.2+7-67, Different versions may vary slightly .
Use jconsole Data can be observed in real time , And it's not just about observing local processes , You can also observe remote processes .
The method is simple to use . Enter... At the command terminal jconsole It will pop up automatically .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-27AjN2Zs-1656678910764)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630213435635.png)]
The observations of the above code are as follows .

summary :
The above is for troubleshooting , We add log as well as sleep The way , Help us with dump Grasp of time , Similar methods can be used in actual production .
Except for memory ,jconsole You can also monitor threads 、cpu Occupancy rate and the number of classes change .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-J3O31REj-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630214317498.png)]
It can also help us detect deadlocks .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-98hyGDJs-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630214429419.png)]
6.4 The memory occupation of multiple garbage collection is still very high, and the troubleshooting of the problem
jvisualvm It's also a visualization tool , Than jconsole Better to use .
The method is simple to use , Use command which java Find the java The installation path , And switch to this path , Then execute on the corresponding path jvisualvm that will do .
In the high version JDK( Greater than 1.8 Or later updated 1.8 edition ) It will no longer be automatically integrated in . The reference blog can download the independent version :JDK There is no higher version VisualVM_ Dongli Jike's blog -CSDN Blog .
Now use it to demonstrate , The memory occupation of multiple garbage collection is still very high, and the troubleshooting of the problem
/** * Show me how many objects to view Heap dump dump */
public class Demo1_13 {
public static void main(String[] args) throws InterruptedException {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 200; i++) {
students.add(new Student());
// Student student = new Student();
}
Thread.sleep(1000000000L);
}
}
class Student {
private byte[] big = new byte[1024*1024];
}
Use jvisualvm Click to select the corresponding process , You can see that the heap memory is very high , Carry out garbage collection .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-gCeFjrEJ-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630215736401.png)]
Memory has not been reduced much .

Conduct Heap Dump, Select the right menu bar , According to the occupied memory class Sort
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OXUm6CVo-1656678910766)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630215949114.png)]
According to the occupied memory class The sorting results are as follows .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-fncnRUPN-1656678910766)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630220215653.png)]](/img/2b/d887e5a185508da339ecbb90210670.png)
Click the class to see all instances of the corresponding class , We click on the one that occupies the most memory ArrayList Check out the examples , Click elementData You can see the details .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-d6CYzWsS-1656678910766)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630220457940.png)]](/img/af/4ab245946c1a377d84514c32638e0b.png)
The discovery may be student The pot , You can see some examples , Confirm the conclusion .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-FAioXX3j-1656678910766)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630220627362.png)]](/img/1a/1d46f9a09d193f7a3de7086cf254c8.png)
Point again student object , Navigate to the property big. It is found that an attribute takes about 1M Space . I can't stand it
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-lbU4siyo-1656678910767)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630220915782.png)]](/img/de/1ba3cbb5d1e1d103f9e2053b9545da.png)
Then locate the corresponding source code .
/** * Show me how many objects to view Heap dump dump */
public class Demo1_13 {
public static void main(String[] args) throws InterruptedException {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 200; i++) {
students.add(new Student());
// Student student = new Student();
}
Thread.sleep(1000000000L);
}
}
class Student {
private byte[] big = new byte[1024*1024];
}
Sure enough , We have 200 individual Student object , Holds the memory footprint 1M Of big example 200 individual , And has been living .
边栏推荐
- Redis (7) -- database and expiration key
- 徹底搞懂基於Open3D的點雲處理教程!
- Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- Singapore summer tourism strategy: play Singapore Sentosa Island in one day
- 什么是云原生?这回终于能搞明白了!
- AI开发调试系列第二弹:多机分布式调测探索之旅
- 学习八股文的知识点~~1
- 如何优雅的写 Controller 层代码?
- 深度神经网络总结
猜你喜欢

Tips for material UV masking

UML class diagram

医院在线问诊源码 医院视频问诊源码 医院小程序源码

Yesterday, Alibaba senior wrote a responsibility chain model, and there were countless bugs

Thoroughly understand the point cloud processing tutorial based on open3d!

M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )

什么是云原生?这回终于能搞明白了!

夜神模擬器+Fiddler抓包測試App

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
随机推荐
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
电商系统中常见的 9 大坑,你踩过没?
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
Uncover the whole link communication process of dewu customer service im
How to clean up discarded PVs and their corresponding folders
The difference between promise and observable
Installation of thingsboard, an open source IOT platform
How to copy and paste interlaced in Excel
Leetcode(154)——寻找旋转排序数组中的最小值 II
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
Leetcode interview question 16.17 Continuous sequence
哪个券商公司网上开户佣金低又安全又可靠
Deep neural network Summary
一款简约PHP个人发卡程序V4.0版本
300+ documents! This article explains the latest progress of multimodal learning based on transformer
MySQL about only_ full_ group_ By limit
options should NOT have additional properties
Eliminate the yellow alarm light on IBM p750 small computer [easy to understand]