当前位置:网站首页>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)
边栏推荐
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- Music playback (toggle & playerprefs)
- RTKLIB: demo5 b34f. 1 vs b33
- 国企秋招经验总结
- Compile GDAL source code with nmake (win10, vs2022)
- 2年经验总结,告诉你如何做好项目管理
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- GNSS positioning accuracy index calculation
- Excel导入,导出功能实现
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
猜你喜欢
Fabrication d'un sac à dos simple fairygui
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Rt-ppp test using rtknavi
Implementation of Excel import and export functions
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
rtklib单点定位spp使用抗差估计遇到的问题及解决
[算法] 剑指offer2 golang 面试题10:和为k的子数组
The port is occupied because the service is not shut down normally
[algorithm] sword finger offer2 golang interview question 1: integer division
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
随机推荐
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
异常:IOException:Stream Closed
rtklib单点定位spp使用抗差估计遇到的问题及解决
wsl常用命令
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Database table splitting strategy
错误:排序与角标越界
RTKLIB: demo5 b34f.1 vs b33
FairyGUI摇杆
Novatel board oem617d configuration step record
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
音乐播放(Toggle && PlayerPrefs)
2022 National Games RE1 baby_ tree
The earth revolves around the sun
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Record: solution of 404 error of servlet accessing database in dynamic web project
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Pride-pppar source code analysis