当前位置:网站首页>Several high-frequency JVM interview questions
Several high-frequency JVM interview questions
2022-07-06 13:03:00 【Java misty rain】
Preface
Hello everyone . I sorted out several high-frequency JVM Interview questions , Guys, come on !
JVM Why is memory divided into new generations , Old age
Why is the new generation divided into Eden and Survivor
JVM Once complete GC How is the process
CMS Collectors and G1 The difference between collectors
JVM tuning
CPU How to check
1. JVM Why is memory divided into new generations , Old age ?
1.1 JVM Shared memory partition
Shared memory area = Lasting generation + Pile up (jdk1.8 And above jvm Abandoned the lasting generation )
Lasting generation = Method area + other
Java Pile up = Old age + The new generation
The new generation = Eden + S0 + S1
1.2 Why is it divided into the old generation and the new generation
The new generation : It mainly stores newly created objects , Memory size is usually small , Garbage collection will be more frequent .
Old age (Tenured Gen): Main storage JVM Objects with a long life cycle ( After several times of Young GC After recycling, it still exists ), Or big objects , Garbage collection is also relatively less frequent .
Why divide the old age and the new generation , The main The object size is different , Object life cycle is different . After division , Provide garbage collection efficiency , Save resources , Improve object utilization wait .
2. Why is the Cenozoic divided into Eden and Survivor? Why set two Survivor
without Survivor,Eden Every time the district goes on Minor GC, The surviving object will be sent to the old age . The old generation is soon filled , Trigger Major GC. The memory space of the old generation is much larger than that of the new generation , Do it once. Full GC The ratio of time spent Minor GC Much longer , So it needs to be divided into Eden and Survivor.
Survivor The meaning of being , Is to reduce the number of people sent to the old age , And then reduce Full GC Happen ,Survivor Pre screening guarantee , Only experience 16 Time Minor GC Objects that can survive in the new generation , Will be sent to the old age .
Set the two Survivor The biggest advantage of the zone is that it solves fragmentation , The newly created object is Eden in , Experience once Minor GC,Eden The live object in will be moved to the first piece survivor space S0,Eden Be emptied ; etc. Eden The district is full again , Just trigger it again Minor GC,Eden and S0 The live objects in will be copied to the second block survivor space S1( This process is very important , Because this replication Algorithm To ensure the S1 From S0 and Eden Two live objects occupy continuous memory space , To avoid fragmentation )
3. JVM Once complete GC How is the process
Java The heap is divided into old and new generations
The new generation Divided into Eden And two Survivor(S0、S1)
When Eden The area is full of space , Java A virtual opportunity triggers Minor GC, To collect the new generation of garbage , Surviving objects , It will be transferred to Survivor District .
Big object ( Need a lot of continuous memory space Java object , Like that long string ) Go straight into old age ;
If the object is Eden born , And after the first time Minor GC Still alive after , And be Survivor If it can be accommodated , Age is set to 1, Every time I get through Minor GC, Age +1, If the age exceeds a certain limit (15), They are promoted to old age . That is, the long-term survival of the object into the old state .
The old generation is full and unable to accommodate more objects ,Minor GC After that it usually goes on Full GC,Full GC Clean up the entire memory heap – Including the young and the old .
Major GC It happened in the old days GC, Clean up the old age area , Often accompanied at least once Minor GC, Than Minor GC slow 10 More than times .
4. CMS Collectors and G1 The difference between collectors
CMS Collector is the collector of the old days , Generally with the new generation Serial and ParNew Collector used together ;G1 Collector collection range is old and new generation , It does not need to be used in combination with other collectors ;
CMS A collector is a way to get Minimum recovery pause time Collector for target , G1 The collector Predictable garbage collection pause time .
CMS Collector is used “ Mark - eliminate ” Algorithm Garbage collection , It is easy to generate memory fragmentation ; and G1 The collector uses “ Mark - Arrangement ” Algorithm , Space integration , Reduced memory space fragmentation .
CMS and G1 The recycling process is different , The garbage collection process is different .CMS yes Initial marker 、 Concurrent Tags 、 Re label 、 Concurrent cleanup ;G1 Is the initial tag 、 Concurrent Tags 、 Final marker 、 Screening and recovery .
5. JVM tuning
JVM Tuning is actually through tuning JVM Parameters , That is, the optimization of garbage collector and memory allocation , To achieve higher throughput and performance .JVM Tuning mainly adjusts the following parameters
Stack memory dependent
-Xms Set the size of the initial heap
-Xmx Set the maximum heap size
-Xmn Set younger generation size , It is equivalent to configuring at the same time -XX:NewSize and -XX:MaxNewSize For the same value
-Xss Stack size per thread
-XX:NewSize Set younger generation size (for 1.3/1.4)
-XX:MaxNewSize Young generation maximum (for 1.3/1.4)
-XX:NewRatio The ratio of the younger generation to the older generation ( Remove the permanent generation )
-XX:SurvivorRatio Eden District and Survivor The ratio of the region
-XX:PretenureSizeThreshold When the created object exceeds the specified size , Assign objects directly to the older generation .
-XX:MaxTenuringThreshold Set the object in Survivor Maximum age threshold for replication , Over the threshold to the old age
Garbage collector related
-XX:+UseParallelGC: Choose garbage collector as parallel collector .
-XX:ParallelGCThreads=20: Configure the number of threads for the parallel collector
-XX:+UseConcMarkSweepGC: Set up concurrent collection of elderly generation .
-XX:CMSFullGCsBeforeCompaction=5 Because the concurrent collector does not compress the memory space 、 Arrangement , So running for a while will produce “ debris ”, Reduce the efficiency of operation . This value sets to run 5 Time GC Compress the memory space later 、 Arrangement .
-XX:+UseCMSCompactAtFullCollection: Turn on the compression of the old . May affect performance , But it can eliminate debris
Auxiliary information related
-XX:+PrintGCDetails Print GC Details
-XX:+HeapDumpOnOutOfMemoryError Give Way JVM Automatically generate memory snapshot in case of memory overflow , Check the problem with
-XX:+DisableExplicitGC Disable the system System.gc(), To prevent manual triggering FGC Cause problems .
-XX:+PrintTLAB see TLAB Use of space
6. CPU How to check
Input jps, Get the process number .
top -Hp pid Get the... Of all threads in this process CPU Time consuming performance
printf %x cpu The highest thread ( Turn into 16 Base number )
jstack Command to view the current java The stack state of the process | grep that 16 Base number
perhaps jstack -l > /tmp/output.txt Type the stack information into a txt file .
If this article helps you , Don't forget to give me a 3 even , give the thumbs-up , forward , Comment on ,
I'll see you next time ! How to get answers : Liked Commented Closed ~
Learn more knowledge and skills , Follow up with private bloggers (03)
边栏推荐
- PRIDE-PPPAR源码解析
- 基于rtklib源码进行片上移植的思路分享
- MySQL shutdown is slow
- FairyGUI簡單背包的制作
- Excel导入,导出功能实现
- 微信小程序开发心得
- 基本Dos命令
- Realization of the code for calculating the mean square error of GPS Height Fitting
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
猜你喜欢
Fairygui loop list
rtklib单点定位spp使用抗差估计遇到的问题及解决
Office prompts that your license is not genuine pop-up box solution
Role movement in the first person perspective
[Chongqing Guangdong education] Shandong University College Physics reference materials
Halcon knowledge: gray_ Tophat transform and bottom cap transform
Basic DOS commands
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
2年经验总结,告诉你如何做好项目管理
随机推荐
编辑距离(多源BFS)
GNSS定位精度指标计算
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
Dark chain lock (lca+ difference on tree)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
MySQL backup -- common errors in xtrabackup backup
341. Flatten nested list iterator
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
国企秋招经验总结
平衡二叉树详解 通俗易懂
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
Record: Navicat premium can't connect to MySQL for the first time
Fabrication of fairygui simple Backpack
Fairygui character status Popup
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
Code example of MATLAB reading GNSS observation value o file
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
基于rtklib源码进行片上移植的思路分享
Rt-ppp test using rtknavi
如何保障 MySQL 和 Redis 的数据一致性?