当前位置:网站首页>JVM series (3) -- memory allocation and recycling strategy
JVM series (3) -- memory allocation and recycling strategy
2022-04-23 05:48:00 【The green flowers of Wang Li's family】
We follow the above JVM series (2)—— Garbage collection Continue to summarize . This article focuses on memory allocation and recycling strategies .
One 、 Generational collection algorithm
The memory is divided into several blocks according to the life cycle of the object ( In the pile ).
(1) In the new generation , A large number of objects are found dead in every garbage collection , Only a few survive , Then choose the replication algorithm , Only a small amount of the cost of replication of living objects can complete the collection .
(2) In the old age , Because of the high survival rate of objects 、 There is no additional space to guarantee its distribution , You have to use a marker - Clean up algorithms or tags - Organize algorithms to recycle .
Two 、 Memory allocation
Memory allocation , It mainly talks about the allocation of objects on the heap ( It may also be allocated on the stack ):
(1) The objects are mainly preferentially distributed in the Cenozoic Eden Suprazonal ,
(2) If the local thread allocation buffer is started , Will prioritize by thread on TLAB The distribution of .
(3) A few objects may also be allocated directly to the elderly , Large objects go directly into the old generation , Long term survivors enter the old age .
2.1 Give priority to the new generation of Eden District
Eden: Translated as the garden of Eden . literally , It represents beauty 、 Freshmen . On the other hand , That's where the object is born .
Most of the time , Objects are assigned to the new generation of Eden District . When Eden When the area does not have enough memory space to allocate , A virtual opportunity is launched Minor GC, Recycling the new generation Eden Garbage objects in the area .
Minor GC: It's garbage recycling that only happens in the new generation ,Java Most objects have the characteristics of living in the morning and dying in the evening ( It works very fast , He died quickly ), therefore Minor GC Very often , Generally, the recovery speed is also very fast ;
(1)-XX:+PrintGCDetails
Configure collector log parameters , Notify the virtual machine to print the memory recycling log when garbage collection occurs , And output the current memory regions when the process exits
Distribution .
(2)-XX:NewRatio=4
Indicates setting the younger generation : The size ratio of the old age is 1:4, This means that the younger generation accounts for 1/5
(3)-XX:SurvivorRatio=8
Presentation settings 2 individual Survivor District :1 individual Eden The area size ratio is 2:8, It means Survivor District accounts for the whole young generation 1/5, This parameter defaults to
by 8
(4)-Xmn20M
Indicates that the size of the younger generation is set to 20M
2.2 Threads take precedence in TLAB The distribution of
Multiple threads apply for space on the heap at the same time , And the heap needs to be synchronized , Can cause performance degradation .
Thread Local Allocation Buffer: Threads allocate the cache locally , From the name, it is a thread specific memory allocation area , It's designed to speed up object allocation . Each thread generates one TLAB, A work area that is exclusive to this thread .(TLAB It was piled up Eden In the area )
Set parameters :
-XX:+UseTLAB: open TLAB( On by default ).
-XX:TLABWasteTargetPercent Set up TLAB Space occupied Eden Percentage size of space .
2.3 Old age
1、 Large objects go directly into the old generation
Need a lot of continuous memory space Java Objects are called large objects , The frequent occurrence of large objects is easy to trigger garbage collection in advance to obtain more continuous space for the allocation of large objects . When writing programs, try to avoid “ A short-lived man ”.
-XX:PretenureSizeThreshold To set the threshold for the object to enter the old age directly , When the object is greater than this value, it directly enters the elderly generation , To do so
It's to avoid being in Eden Area and Survivor There are a lot of memory copies between regions .
-XX:PretenureSizeThreshold=3145728
Indicates that the object is greater than 3145728(3M) Directly enter the distribution of the elderly generation , Only bytes can be used here
2、 Long term survivors will enter the old age
(1) The virtual machine defines an age counter for each object . The object was born in Eden District 、 After a Minor GC Still alive after , And can be Survivor accommodate , Set age to 1, The object is Survivor The area passes once at a time Minor GC, Age adds 1, When age reaches a certain level ( Default 15), I'll be promoted to the old age .
-XX:MaxTenuringThreshold=1
Indicates that the object is older than 1, Automatically enter the elderly generation
(2) Dynamic object age determination : The object's age has reached MaxTenuringThreshold Can enter the old generation , meanwhile , If in survivor The sum of the size of all objects of the same age in the region is greater than survivor Half of the district , Those who are older than or equal to this age can directly enter the elderly generation . No need to wait MaxTenuringThreshold The age required in .
Major GC: Refers to what happened in the old days GC, There is Major GC, often It will be accompanied at least once Minor GC( But not absolutely , stay ParallelScavenge In the collection strategy of the collector, there is a direct process Major GC The strategy selection process ).Major GC It's usually faster than Minor GC slow 10 More than times .
Full GC: When the old generation is full, it will cause Full GC,Full GC The young generation will be recycled at the same time 、 Old age .
3、 ... and 、 Space allocation guarantee mechanism
As shown in the figure :
Reasons for adopting space allocation guarantee mechanism :
The new generation uses replication collection algorithm , But for memory utilization , Use only one Survivor Space as a rotation backup , If a large number of objects are in Minor GC Still alive after ( The most extreme case is that all objects in the Cenozoic survive after memory recycling ), and Survivor The space is relatively small , At this time, it is necessary for the elderly generation to distribute guarantee , hold Survivor Objects that cannot be accommodated are put into the old age .
The elderly generation should allocate space , The premise is that the elderly have enough space to accommodate these objects , But it is unpredictable how many objects will survive memory reclamation , So we have to take The average value of the object size promoted to the old age after each garbage collection As a reference . Use this average to compare with the remaining space of the elderly generation , To decide whether to proceed with Full GC To make room for the elderly .
Four 、 Assign priority to objects
As shown in the figure :
(1) On the stack : after JIT After compiling , Break up thread private objects that cannot be accessed by other threads ( Disaggregate to scalar type ) Indirectly allocated on the stack , Instead of being allocated on the heap .
(2) Assign priority as shown in the figure .
版权声明
本文为[The green flowers of Wang Li's family]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230540318019.html
边栏推荐
- 软件架构设计——软件架构风格
- C语言——恶搞关机小程序
- 建表到页面完整实例演示—联表查询
- The list attribute in the entity is empty or null, and is set to an empty array
- Frequently asked interview questions - 3 (operating system)
- JS number capitalization method
- Record a project experience and technologies encountered in the project
- 【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)
- ES6之解构函数
- MySQL创建oracle练习表
猜你喜欢
Flutter nouvelle génération de rendu graphique Impeller
mysql实现主从复制/主从同步
Deep learning object detection
2 - principes de conception de logiciels
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
Find the number of "blocks" in the matrix (BFS)
opensips(1)——安装opensips详细流程
MySQL的锁机制
2-軟件設計原則
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
随机推荐
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
PHP处理json_decode()解析JSON.stringify
热键,界面可视化配置(界面交互)
freemark中插入图片
Breadth first search topics (BFS)
提升Facebook触及率和互动率攻略 | 智能客服帮您抓住用户的心
合约锁仓漏洞
Summary of redis classic interview questions 2022
io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
JDBC连接数据库
MySQL triggers, stored procedures, stored functions
protected( 被 protected 修饰的成员对于本包和其子类可见)
refused connection
Frequently asked interview questions - 2 (computer network)
2-软件设计原则
opensips(1)——安装opensips详细流程
What is JSON? First acquaintance with JSON
Relative reference and absolute reference of Excel
多线程与高并发(2)——synchronized用法详解
Step on the pit: Nacos uses startup CMD - M standalone startup error