当前位置:网站首页>JVM garbage collection
JVM garbage collection
2022-07-05 03:55:00 【Long time】
Garbage collection
1 How to judge whether an object can be recycled
1.1 Reference counting
seeing the name of a thing one thinks of its function , Every time it is quoted +1;
In the case of circular references , Circular reference objects will not be recycled , Memory leak .
1.2 Reachability analysis algorithm
Java The garbage collector in virtual machine uses reachability analysis to explore all living objects ; Scan the objects in the team , See if you can follow GC Root object ( Root object ) Find the object for the reference chain of the starting point , Not found, indicating that it can be recycled . That is, first determine the root object , By root object Indirect or direct reference None of them can be recycled .
Which objects can be used as root objects
- Virtual machine stack ( Local variable table in stack frame ) Object referenced in
- Local method stack Native Method reference object
- Object referenced by a class static property in a method area as well as Objects referenced by constants
1.3 Four quotations
1. Strong citation
- adopt new Keywords are strong references
- Only all GCRoots object Not through 【 Strong citation 】 Reference the object , This object can only be garbage collected
2. Soft citation (SoftReference)
- Only soft references When referencing this object , stay After recycling , When memory is still insufficient Will start garbage collection again , Recycling soft reference objects , You can release the soft reference itself in conjunction with the reference queue
3. Weak reference (WeakReference)
- When only weak references refer to the object , During garbage collection , Whether the memory is sufficient or not , Will recycle weak reference objects , The weak reference itself can be released by cooperating with the reference queue
4. Virtual reference (PhantomReference)
- Must be used with reference queues , Mainly with ByteBuffer Use , When the referenced object is recycled , I'm going to join the team , from Reference Handler Thread calls virtual reference related methods to release direct memory – That is to call Cleaner Reclaim direct memory according to the recorded direct memory address
5. Terminator references (FinalReference)
- No need to code manually , But it's used internally with reference queues , During garbage collection , Terminator references are queued ( The referenced object has not been recycled yet ), Again by Finalizer The thread finds the referenced object through the terminator reference and calls its finalize Method , The second time GC The referenced object can be recycled only when the
Soft reference application
public static void soft() {
//list-> SoftReference--> byte[]
List<SoftReference<byte[]>> list= new ArrayList<>();
for(int i=0;i<5;i++){
SoftReferencec<byte[]> ref=new SoftReference<>(new byte[_4MB]);
System.out.println(ref.get());
list.add(ref);
System.outprintln(list.size())
}
System.out.println(" End of looping :"+list.size());
for(SoftReference<byte[]> ref :list){
System.out.println(ref.get());
}
}
Use Reference queue Clear useless soft references
private static final int_4MB=4* 1024* 1024;
public static void main(string[] args){
List<SoftReference<byte[]>> list = new ArrayList<>();
ReferenceQueue<byte[]> queue=new ReferenceQueue<>();// Reference queue
for(int i=0;i<5;i++){
// Associated with the reference queue , When a soft reference is associated with byte[] When it's recycled , The soft reference itself will be added to queue In the middle
SoftReference<byte[]> ref=newSoftReference<>(new byte[_4MB],queue);
System.out.println(ref.get());
list.add(ref);
System.out.println(list.size());
}
// Get useless... From the queue Soft reference objects , And remove
Reference<? extends byte[]>poll=queue.poll();
while( poll != null) {
list.remove(poll);
poll = queue.poll();
}
System.out.println("========");
for(SoftReference<byte[]> reference :list){
System.outprintln(reference.get());
}
}
Weak reference application
public static void soft() {
//list-> WeakReference--> byte[]
List<WeakReference<byte[]>> list= new ArrayList<>();
for(int i=0;i<5;i++){
WeakReference<byte[]> ref=new WeakReference<>(new byte[_4MB]);
System.out.println(ref.get());
list.add(ref);
for(WeakReference<byte[]> w :list){
System.out.println(w.get());
}
}
}
2 Garbage collection algorithm
2.1 Mark clear
Mark all objects that need to be recycled , Recycle marked objects
- Faster
- Will cause memory fragmentation
2.2 Tag to sort out
Mark all objects that need to be recycled , Recycle marked objects , Put the memory of unmarked objects Put it in order Continuous memory space , But this process will change the address of the object , thus
- Slow speed
- No memory fragmentation
2.3 Copy
Divide memory into two equal sized blocks , Use only one piece at a time . When it is almost used up, garbage collection is triggered : Mark all objects that need to be recycled , Copy the unmarked object to another memory space , Recycle the existing memory space at one time ; The next time you trigger garbage collection, copy another piece of live garbage to this piece , Recycle another piece at one time , cycle .
- There will be no memory fragmentation
- It takes up double the memory space , Memory utilization is not high
2.4 Generational algorithm
Divide the memory area into the new generation and the old generation , Adopt different algorithms for different regions :
- The new generation adopts replication algorithm , In the old days, we adopted marking and sorting / Tag clearing algorithm
- Objects are first assigned to the Eden area
- When the new generation is short of space , Trigger new generation recycling (Minor GC), The object of the garden of Eden Copy to the surviving area , The age of the survivors plus 1; Next trigger Minor GC when , The garden of Eden and the surviving objects , The age of the survivors plus 1, Copy to another surviving area , cycle
- From And To It can be understood as from this surviving area From Copy to another surviving area To, Therefore, the name of the surviving area is not fixed , The author believes that it is only for convenience of memory
- Minor gc May trigger stop the world(STW), Pause other users' threads , When the garbage collection is over , The user thread will resume running
- When the object lifetime exceeds a threshold , Will be promoted to the old age , Maximum life 15(4bit)
- When the old days were short of space , Will try to trigger the new generation recycling first , If there's not enough space after that , It triggers Full GC,STW Longer time
Be careful :
If the operation of other threads will not affect the data of the main thread , Then the memory of other threads overflows , It will not affect the normal operation of the main thread
3 Garbage collector
In general, there are four kinds of garbage collectors : Serial 、 parallel 、 Concurrent 、G1
3.1 Serial
For single thread 、 Heap memory is small
Will pause all other threads
Serial The collector : Copy the new generation (Serial), Take marking and sorting for the elderly (Serial Old)
Corresponding JVM Parameters :-XX:+UseSerialGC
3.2 parallel
- Multiple garbage collection threads work in parallel , Will pause all other threads
- Suitable for weak interaction
ParNew The collector : Only for the new generation of recycling , yes Serial A new generation of parallel multithreaded collectors , Also copy the Cenozoic .
Corresponding JVM Parameters :-XX:+UseParNewGC – ParNew + Serial Old The collector combination of
Parallel The collector : Copy the new generation (Parallel Scavenge), Take marking and sorting for the elderly (Parallel Old).
Parallel Scavenge The collector : similar ParNew The collector is also a parallel multithreaded garbage collector , Commonly known as throughput first collector .
throughput : Run user code time Proportion ( Run user code time + Garbage collection time )
If the program runs 10 minute , Garbage collection 5 second , The throughput is about 99%
Parallel Old The collector : yes SerialOld Parallel multithreaded versions of collectors from older generations
Corresponding JVM Parameters :-XX:+UseParallelGC or -XX:+UseParallelOldGC – Parallel Scanvenge + Parallel Old The collector combination of
ParNew The collector And Parallel Scavenge The collector difference – Parallel Scavenge Collector's Adaptive strategy
- The virtual machine collects performance monitoring information based on the current system performance , Dynamically adjust parameters to provide the most appropriate pause time (-XX:MaxGCPauseMillis) Or maximum throughput
3.3 Concurrent
Garbage collection threads and other threads can execute concurrently , Don't stop other threads
It is applicable to , Heap memory is large ,CPU Audit support
Old age concurrent mark removal
CMS The collector :Concurrent Mark Sweep: Concurrent tag removal , It is a kind of collector that aims to get the shortest recovery pause time .
- Initial marker : Mark the objects directly referenced by the root object , Meeting STW That is, stop other threads
- Concurrent Tags : Find the surviving object simply referenced by the root object , Other threads execute concurrently
- Re label : Fix the mark record of the part of the object whose mark changes due to the continuous operation of other threads during concurrent marking , Meeting STW
- Concurrent elimination : Recycle the object to be recycled
Corresponding JVM Parameters : -XX:+UseConcMarkSweepGC ( Automatically put -XX:+UseParNewGC open )–
ParNew + CMS + Serial Old The collector combination of ,Serial Old Will serve as a CMS Fallback collectors
Be careful :
- Can't handle floating garbage , Judge that the object is not garbage before , Because the user thread is also running , Maybe the object has become a garbage object before it is cleared , Eventually, it should have been recycled but not recycled , Just wait for the next time GC Then recycle the object
- If CMS The memory reserved during operation cannot meet the needs of the program , Will appear Concurrent Mode Failure, Demote temporarily enabled Serial Old Collector for old age garbage collection ,STW So the time becomes longer
3.4 G1
G1 Applicable to the whole stack , It can be used in the new generation and the old generation
Pay attention to throughput and low latency at the same time , The default pause is 200ms
Huge amount of memory , The heap will be divided into multiple equal sized region, It can be done by -XX:G1HeapRegionSize Parameter setting area size , Must be 2 Power square
The whole is marked + Sorting algorithm , Two region Between regions is the replication algorithm
G1 The collector : A garbage collector with the process of sorting memory , There won't be a lot of memory fragmentation ;STW More controllable , A prediction mechanism is added to the pause time , The user can specify the expected pause time .
It can be divided into the following four steps :
Initial marker : Mark the objects directly referenced by the root object , Meeting STW That is, stop other threads ;
And modify TAMS Value , Let the next phase of the user program run concurrently , Can be used in the right Region Create a new object in .
Every Region There are two records top-at-mark-start (TAMS) The pointer , Respectively
prevTAMS
andnextTAMS
, stay TAMS The object corresponding to the above memory space is newly allocatedConcurrent Tags : Find the surviving object simply referenced by the root object , Other threads execute concurrently
Final marker : Fix the mark record of the part of the object whose mark changes due to the continuous operation of other threads during concurrent marking , Meeting STW
Screening and recovery : To each Region Sorted by recovery value and cost , According to the user's expectation GC Pause time to plan recovery , Take the part that you decide to recycle Region Of live objects copied to empty Region in , And clean up the whole old Region The whole space of , Meeting STW
Garbage collection stage
New generation recycling : Conduct GC Root The initial tag of , Meeting STW;
New generation recycling + Concurrent Tags : When the proportion of heap space occupied by the old age reaches the threshold , Mark concurrency ( Can't STW);
Mixed recycling : Carry out comprehensive recycling , The final mark and copy survive , Meeting STW.( Within the maximum stop time , More heap space is released for garbage collection , Will choose a larger memory space
Cross generational quotation
The old times quote the new generation objects
During garbage collection, accessibility analysis needs to be carried out according to the root object , And the old age may also have root objects , Cross generational references do not need to completely traverse older generations , But by traversing Dirty card Get the root object .
Class unload
After all objects are marked concurrency , You can know which classes are no longer used , When all classes of a class loader are no longer used , Then unload all the classes it loads .
Recycling giant objects
An object is larger than region Half the time , Call it a giant object
G1 You don't copy giant objects
Recycling is a priority
G1 I'll track all the things in the old days incoming quote , So the older generation incoming Referenced as 0 Giant objects can be disposed of in the Cenozoic
4 Garbage collection tuning
Tuning areas
- Memory
- Lock competition
- CPU Occupy
- io
Set goals
Low latency or high throughput , Choose the right recycler
- CMS G1 ZGC – Low latency
- ParallelGC -- High throughput
- Zing virtual machine – Low latency
4.1 One of the fastest GC It doesn't happen GC
Because if it happens frequently GC That explains. STW The longer it takes , Then it is also detrimental to the operation of the program .
see FullGC Memory usage before and after , Consider the following questions
Is there too much data
Such as database operation result set , Loaded some unnecessary data , It can be done by limit Limit
Is the data representation too bloated
Is there a memory leak
static Map map Objects of this length
Soft citation / Weak reference
Third party cache implementation Such as middleware redis
4.2 Cenozoic optimization
- The characteristics of the new generation
- be-all new The memory allocation of the operation is very complicated
- The cost of recycling dead objects is 0
- Most of the objects die after use
- New generation recycling Minor GC Time is far less than Full GC
The larger the memory of the new generation, the better :
- Small : You may start frequently Minor GC;Oracle It is recommended to occupy heap memory 25%-50%;
- Too big : Then the longer it takes to recycle .
Ideal situation :
The new generation can hold all 【 Concurrency *( request - Respond to )】 The data of
The surviving area is large enough to be preserved 【 The current active object + People who need to be promoted 】
If the survival area is too small , Will automatically adjust the promotion threshold , You may promote the object to the old age in advance , Impact : Objects that originally had a short life span had to wait Full GC It can only be recycled
Promotion thresholds are properly configured , Let the long-term survivors be promoted as soon as possible
Minor GC Mainly marking and copying , It mainly consumes time on replication ; If a long-lived object cannot be promoted as soon as possible , That means it will be copied back and forth in the surviving area , It's a burden for performance .
4.3 In the old days
With CMS For example
CMS The garbage collector has two defects , One is the inability to dispose of floating garbage , That is, in the process of concurrent cleanup , User threads generate new garbage , These garbage can't be marked and can only wait until next time FullGC Clean up . Therefore, in the old age, a certain space needs to be reserved for floating garbage . stay CMS The process may lead to concurrency failure , namely CMS Runtime memory space cannot be satisfied , Then the virtual machine will Serial Old Take it out and STW, Carry out serial garbage cleaning .
CMS The older the memory, the better , Avoid concurrent failures caused by floating garbage
Try not to tune first , without Full GC That means there is no lack of memory space in the old age , Even if it happens Full GC, Try tuning the new generation first
Watch what happens Full GC when , Memory usage in old times , Turn up the memory of the old age 1/4 ~1/3, Reduce Full GC Happen
-XX:CMSInitiatingOccupancyFraction=percent Adjust startup CMS The ratio of used memory in old age
Indicate the : Picture source letter station JVM Series video screenshots
边栏推荐
- Installation of postman and postman interceptor
- [安洵杯 2019]不是文件上传
- Use of vscode software
- @Transactional 注解导致跨库查询失效的问题
- Timing manager based on C #
- [wp]bmzclub writeup of several questions
- 面试汇总:这是一份全面&详细的Android面试指南
- 阿里云ECS使用cloudfs4oss挂载OSS
- Containerization Foundation
- Operation flow of UE4 DMX and grandma2 onpc 3.1.2.5
猜你喜欢
[luat-air105] 4.1 file system FS
grandMA2 onPC 3.1.2.5的DMX参数摸索
The new project Galaxy token just announced by coinlist is gal
[an Xun cup 2019] not file upload
【无标题】
Quick start of UI component development of phantom engine [umg/slate]
Yuancosmic ecological panorama [2022 latest]
error Couldn‘t find a package. JSON file in "your path“
MindFusion.Virtual Keyboard for WPF
Installation of postman and postman interceptor
随机推荐
Use of vscode software
Clickhouse synchronization MySQL (based on materialization engine)
[learning notes] month end operation -gr/ir reorganization
Kubernetes - identity and authority authentication
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)
Analysis of dagger2 principle
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
Web components series (VII) -- life cycle of custom components
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
[wp]bmzclub writeup of several questions
Basic function learning 02
英语必备词汇3400
Multimedia query
postman和postman interceptor的安装
Kubernetes - Multi cluster management
A brief introduction to the behavior tree of unity AI
Analysis of glibc strlen implementation mode
Plasticscm enterprise crack
Clickhouse materialized view