当前位置:网站首页>[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.
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 .
jps
See what the system has java processjmap
Check the occupation of heap memory at a certain timejconsole
Multifunctional 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 instead
Because 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 .
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 .
The discovery may be student The pot , You can see some examples , Confirm the conclusion .
Point again student object , Navigate to the property big. It is found that an attribute takes about 1M Space . I can't stand it
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 .
边栏推荐
- 全链路数字化转型下,零售企业如何打开第二增长曲线
- Uncover the whole link communication process of dewu customer service im
- Is it safe to buy funds on Alipay account
- [Yugong series] July 2022 go teaching course 001 introduction to go language premise
- R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
- CDN acceleration and breaking J anti-theft chain function
- Concepts and differences of PR curve and ROC curve
- 夜神模擬器+Fiddler抓包測試App
- R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
- 【每日一题】第二天
猜你喜欢
How can retail enterprises open the second growth curve under the full link digital transformation
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
Simulateur nightGod + application de test de capture de paquets Fiddler
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
UML class diagram
Redis (7) -- database and expiration key
全链路数字化转型下,零售企业如何打开第二增长曲线
How to clean up discarded PVs and their corresponding folders
医院在线问诊源码 医院视频问诊源码 医院小程序源码
随机推荐
PR曲线和ROC曲线概念及其区别
页面标题组件
Leetcode interview question 16.17 Continuous sequence
How to enable the run dashboard function of idea
AI开发调试系列第二弹:多机分布式调测探索之旅
Meal card hdu2546
元宇宙链游系统开发(逻辑开发)丨链游系统开发(详细分析)
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
How to clean up discarded PVs and their corresponding folders
如何优雅的写 Controller 层代码?
日期工具类(不定时更新)
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
深度神经网络总结
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
2022编译原理期末考试 回忆版
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
Thoroughly understand the point cloud processing tutorial based on open3d!
距离度量 —— 杰卡德距离(Jaccard Distance)
LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());