当前位置:网站首页>[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 .
边栏推荐
- 【愚公系列】2022年07月 Go教学课程 001-Go语言前提简介
- 深度学习数学基础
- 300+ documents! This article explains the latest progress of multimodal learning based on transformer
- R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
- Web实时通信技术之Websocket
- Matlab中弧度转角度、角度转弧度
- “栈”的典型应用—表达式求值(C语言实现)
- The second bullet of AI development and debugging series: the exploration journey of multi machine distributed debugging
- 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
- Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
猜你喜欢
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Thoroughly understand the point cloud processing tutorial based on open3d!
【每日一题】第一天
Mini Golf Course: a good place for leisure and tourism in London
电商系统中常见的 9 大坑,你踩过没?
Redis (7) -- database and expiration key
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
Night God simulator +fiddler packet capture test app
【每日一题】第二天
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
随机推荐
Websocket of Web real-time communication technology
@Component 拿不到dao层
How to set vscode to delete the whole line shortcut key?
Redis(7)----数据库与过期键
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
深度学习数学基础
Introduction to sap s/4hana OData mock service
SQL training 2
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
鸿蒙第四次学习
How to clean up discarded PVs and their corresponding folders
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
Redis (6) -- object and data structure
Leetcode(81)——搜索旋转排序数组 II
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
@Component cannot get Dao layer
昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
Eliminate the yellow alarm light on IBM p750 small computer [easy to understand]
UML class diagram