当前位置:网站首页>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)
边栏推荐
- Containers and Devops: container based Devops delivery pipeline
- Combination of fairygui check box and progress bar
- 闇の連鎖(LCA+树上差分)
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- What are the functions and features of helm or terrain
- Fairygui gain buff value change display
- KF UD decomposition pseudo code implementation advanced [2]
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- 微信小程序开发心得
- Unity3d, Alibaba cloud server, platform configuration
猜你喜欢
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
第一人称视角的角色移动
阿里云一面:并发场景下的底层细节 - 伪共享问题
The port is occupied because the service is not shut down normally
FairyGUI复选框与进度条的组合使用
【干货】提升RTK模糊度固定率的建议之周跳探测
FairyGUI条子家族(滚动条,滑动条,进度条)
《软件测试》习题答案:第一章
Heap sort [handwritten small root heap]
Basic DOS commands
随机推荐
Fundamentals of UD decomposition of KF UD decomposition [1]
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
What are the advantages of using SQL in Excel VBA
Fabrication d'un sac à dos simple fairygui
阿里云一面:并发场景下的底层细节 - 伪共享问题
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Halcon knowledge: gray_ Tophat transform and bottom cap transform
KF UD decomposition pseudo code implementation advanced [2]
地球围绕太阳转
异常:IOException:Stream Closed
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Usage differences between isempty and isblank
染色法判定二分图
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
FairyGUI按钮动效的混用
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
RTKLIB: demo5 b34f. 1 vs b33
如何保障 MySQL 和 Redis 的数据一致性?
《软件测试》习题答案:第一章