当前位置:网站首页>High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
2022-07-06 14:32:00 【Java fruit】
The first choice needs to be understood clearly Straight through BAT Required questions series : Go into detail JVM Memory model and JVM Detailed configuration of parameters ,JVM Memory storage structure and recycling Algorithm , Then, this article gives you a detailed introduction JVM Performance tuning methods and steps .
JVM Memory tuning
Yes JVM The main purpose of memory system level tuning is to reduce GC The frequency and Full GC The number of times .
1.Full GC
The whole pile will be sorted out , Include Young、Tenured and Perm.Full GC Because you need to recycle the entire heap , So it's slower , So it should be reduced as much as possible Full GC The number of times .
2. Lead to Full GC Why
1) The old generation (Tenured) Be full of
When tuning, try to keep objects in the new generation GC It's recycled 、 Keep objects alive for a long time in the new generation and avoid creating too large objects and arrays directly in the old generation .
2) Lasting generation Pemanet Generation The space is insufficient
increase Perm Gen Space , Avoid too many static objects , Control the proportion of new generation and old generation
3)System.gc() Called by display
Do not manually trigger garbage collection , Try to rely on JVM Its own mechanism
In the face of JVM In the process of tuning , A big part of the work is about FullGC The adjustment of , The following details the corresponding JVM Tuning methods and steps .
JVM Performance tuning methods and steps
1. monitor GC The state of
Using a variety of JVM Tools , View the current log , Analyze the present JVM Parameter setting , And analyze the current heap memory snapshot and gc journal , According to the actual area memory partition and GC execution time , Think whether to optimize .
For example : Some phenomena before system crash :
- Every time garbage collection time is longer and longer , By the previous 10ms Extend to 50ms about ,FullGC There's also time before 0.5s Extend to 4、5s
- FullGC More and more times , The most frequent time is less than 1 Every minute FullGC
- Older generations have more and more memory and every time FullGC In the following years, no memory will be released
After that, the system will not be able to respond to new requests , Arrive gradually OutOfMemoryError The critical value of , This is the time to analyze JVM memory dump dump.
2. Make a heap of dump file
adopt JMX Of MBean Generate the current Heap Information , The size is one 3G( The size of the whole pile ) Of hprof file , If not started JMX Can pass Java Of jmap Command to generate the file .
3. analysis dump file
Open this. 3G The heap information file , Obviously, it's just Window The system doesn't have that much memory , High configuration Linux, Several tools open the file :
- Visual VM
- IBM HeapAnalyzer
- JDK Self contained Hprof Tools
- Mat(Eclipse Special static memory analysis tool ) Recommended
remarks : The file is too large , It is recommended to use Eclipse Special static memory analysis tool Mat Open analysis .
4. The results of the analysis , Decide if you need to optimize
If the parameters are set reasonably , There is no timeout log in the system ,GC The frequency is not high ,GC Time consuming is not high , Then there's no need for GC Optimize , If GC For more than 1-3 second , Or frequently GC, You have to optimize .
notes : If the following indicators are met , In general, there is no need for GC:
- Minor GC Execution time is less than 50ms;
- Minor GC Infrequent execution , about 10 Seconds at a time ;
- Full GC Execution time is less than 1s;
- Full GC Execution frequency is not frequent , No less than 10 minute 1 Time ;
5. adjustment GC Type and memory allocation
If the memory allocation is too large or too small , Or use GC The collector is slow , We should adjust these parameters first , And look for 1 One or more machines beta, Then compare the performance of the optimized machine with that of the non optimized machine , And targeted to make the final choice .
6. Constant analysis and adjustment
Through constant trial and error , Analyze and find the most suitable parameters , If the most appropriate parameter is found , Then put these Parameters are applied to all servers .
cms Parameter optimization process
Let me continue to introduce JVM Key parameter configuration of ( For reference only ).
JVM Tuning parameters reference
1. in the light of JVM Heap settings , In general, you can go through -Xms -Xmx Limit it to a minimum 、 Maximum , In order to prevent the garbage collector at the minimum 、 The maximum amount of time required to shrink the pile , Usually put the biggest 、 The minimum setting is the same ;
2. The younger generation and the older generation will be based on the default ratio (1:2) Allocate heap memory , You can adjust the ratio between them NewRadio To adjust the size between the two , It can also be used for recycling .
For example, the younger generation , adopt -XX:newSize -XX:MaxNewSize To set its absolute size . Again , To prevent the young generation from shrinking , We usually put -XX:newSize -XX:MaxNewSize Set to the same size .
3. It's reasonable to set up the young generation and the old generation
1) The younger generation will inevitably lead to the younger generation , The big young generation will extend the average GC The cycle of , But it will increase every time GC Time for ; Small old people lead to more frequent Full GC
2) The younger generation inevitably leads to the older generation , Small young generation will lead to ordinary GC Very frequent , But every time GC Time will be shorter ; There will be fewer older generations Full GC The frequency of
How to choose should depend on the application Distribution of object life cycle : If the application has a large number of temporary objects , We should choose a younger generation ; If there are relatively more persistent objects , Old age should be increased properly . But many applications don't have such obvious features .
When making a choice, we should take root in According to the following two points :
(1) In line with Full GC The principle of minimizing , Let older generation cache common objects as much as possible ,JVM The default scale for 1:2 The same thing .
(2) By observing the application for a period of time , Let's see how much memory the older generation will take up at the peak , It doesn't affect Full GC Under the premise of , According to the actual situation, we should increase the number of young people and replace them with younger ones , For example, we can control the proportion to 1:1. But we should reserve at least 1/3 The growth space of .
4. On better equipped machines ( For example, multicore. 、 Large memory ), You can choose a parallel collection algorithm for the elderly : -XX:+UseParallelOldGC , The default is Serial collect .
5. Thread stack settings : Each thread will be turned on by default 1M The stack , Used to store stack frames 、 Call parameters 、 Local variables, etc , This default is too much for most applications , commonly 256K Just enough .
Theoretically , With constant memory , Reduce the stack per thread , More threads can be generated , But it's actually limited to the operating system .
That's all JVM Summary of performance tuning , In addition to code and application tuning , More importantly, we need to deal with high concurrency from the perspective of architecture design , Using distributed caching 、 Asynchronous message and other ways to solve high concurrency scenarios , The specific contents are as follows :
More topics on high concurrency architecture
How to get the data
Focus on + After forwarding , Private key words 【 High concurrency 】 Can get !
Say the important words twice , forward 、 Send private message after forwarding , You can get it
边栏推荐
- 《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
- Hackmyvm Target Series (3) - vues
- Solutions to common problems in database development such as MySQL
- Sword finger offer 23 - print binary tree from top to bottom
- ES全文索引
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- C language file operation
- Xray and Burp linked Mining
- 《统计学》第八版贾俊平第三章课后习题及答案总结
- servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
猜你喜欢
Build domain environment (win)
Captcha killer verification code identification plug-in
搭建域环境(win)
How to earn the first pot of gold in CSDN (we are all creators)
Hackmyvm target series (3) -visions
内网渗透之内网信息收集(四)
Harmonyos JS demo application development
DVWA (5th week)
外网打点(信息收集)
Windows platform mongodb database installation
随机推荐
《统计学》第八版贾俊平第四章总结及课后习题答案
Intranet information collection of Intranet penetration (I)
我的第一篇博客
HackMyvm靶机系列(2)-warrior
captcha-killer验证码识别插件
Record once, modify password logic vulnerability actual combat
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
内网渗透之内网信息收集(三)
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
外网打点(信息收集)
HackMyvm靶机系列(1)-webmaster
Realize applet payment function with applet cloud development (including source code)
Intranet information collection of Intranet penetration (3)
Detailed explanation of three ways of HTTP caching
安全面试之XSS(跨站脚本攻击)
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
SQL注入
7-14 error ticket (PTA program design)
7-5 staircase upgrade (PTA program design)
Overview of LNMP architecture and construction of related services