当前位置:网站首页>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
边栏推荐
- 转置卷积(Transposed Convolution)
- 基于ssm 包包商城系统
- Golang implements Ping connectivity detection case through exec module
- 7-10 longest symmetric substring (25 points) (violence problem solution) C language
- No.1.#_ 6 Navicat shortcuts
- 2 - principes de conception de logiciels
- Idea plug-in --- playing songs in the background
- MySQL lock mechanism
- The difference between cookie and session
- 建表到页面完整实例演示—联表查询
猜你喜欢
JVM系列(4)——内存溢出(OOM)
多线程与高并发(3)——synchronized原理
deep learning object detection
手动删除eureka上已经注册的服务
Flutter nouvelle génération de rendu graphique Impeller
Establish excel bookkeeping book through setting context menu
JVM系列(3)——内存分配与回收策略
opensips(1)——安装opensips详细流程
Issue 36 summary of atcoder beginer contest 248
SQL statement simple optimization
随机推荐
多线程与高并发(2)——synchronized用法详解
acwing854. Floyd finds the shortest path
线程的底部实现原理—静态代理模式
线性规划问题中可行解,基本解和基本可行解有什么区别?
Flutter 新一代图形渲染器 Impeller
框架解析2.源码-登录认证
Typescript interface & type rough understanding
转置卷积(Transposed Convolution)
Issue 36 summary of atcoder beginer contest 248
MySql基础狂神说
Split and merge multiple one-dimensional arrays into two-dimensional arrays
Strategy for improving the conversion rate of independent stations | recovering abandoned users
Common status codes
字符识别easyocr
Reading notes of modern methods of C language programming
类的加载与ClassLoader的理解
第36期《AtCoder Beginner Contest 248 打比赛总结》
Find the number of "blocks" in the matrix (BFS)
MySQL triggers, stored procedures, stored functions
Object to map