当前位置:网站首页>[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 .
边栏推荐
- How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
- 材质UV遮罩的技巧
- Redis (6) -- object and data structure
- 在Tensorflow2中使用mnist_784数据集进行手写数字识别
- Tips for material UV masking
- Websocket of Web real-time communication technology
- Introduction to sap s/4hana OData mock service
- 阿里三面被面试官狂问Redis,简历上再也不敢写'精通'了
- 文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
- 2022软件工程期末考试 回忆版
猜你喜欢

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

谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展

Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01

Deep learning mathematics foundation

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey

Troubleshooting ideas that can solve 80% of faults
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

Excel如何进行隔行复制粘贴

Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0

昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
随机推荐
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
Competence of product manager
SLAM|如何时间戳对齐?
Leetcode (154) -- find the minimum value II in the rotation sort array
Hongmeng's fourth learning
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
UML 类图
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
How to copy and paste interlaced in Excel
【每日一题】第一天
SQL training 2
Meal card hdu2546
300+ documents! This article explains the latest progress of multimodal learning based on transformer
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
在Tensorflow2中使用mnist_784数据集进行手写数字识别
Leetcode interview question 17.04 Vanishing numbers
Night God simulator +fiddler packet capture test app
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)