<> Cenozoic era GC case
If the system does not stop running , Then put Eden Fill it up ：
It must be triggered at this time Minor
GC, Have special GC Thread execution GC, And there are different garbage collectors for different memory areas , This is equivalent to GC Thread works with the garbage collector , Use your own GC The algorithm executes on the specified memory area GC：
Garbage collection must run through a background GC thread , For example, for new generation ParNew Garbage collector , It adopts replication algorithm for the new generation ： sign Eden Live objects in zone , Then transfer all to Survivor1, Then empty it all at once Eden Garbage object in ：
Then the system continues to run , New objects continue to be assigned in Eden：
When Eden Refill , Trigger again Minor GC, Now it is GC Threads run the algorithm logic in the garbage collector ： Using replication algorithm logic , sign Eden and Survivor1 Live objects in .
Then transfer the living object to Survivor2, Then put Eden and Survivor1 All garbage objects in are recycled ：
<>GC In the process , Can I still create new objects ?
We assume that ：
As shown above , If the garbage collector is trying to put Eden and Survivor2 Mark the surviving objects and transfer them to Survivor1, Then you have to think about Eden
and Survivor2 Clean up all the garbage objects in the , As a result, the system process is still going Eden create new object . Some of these new objects quickly become garbage objects , What now ? It's a mess , These objects are newly created for the program , How do you let the garbage collector keep track of the status of these new objects ?
How can we do it this time GC In the process ：
Transfer those living objects in the new object to Survivor2?
How to recycle the garbage in the newly created object ?
Some students said , That's about the garbage collector ! You have to design well !
Some things are simple , But once you're going to JVM The implementation must be complex . So , stay GC In the process , It also allows us to write Java The system continues to run continuously Eden Continue to create new objects in , It's not appropriate at the moment .
<>JVM Pain points ：Stop the World
because GC Time , Keep the garbage collector as focused as possible , You can't just let us write Java The system continues to create new objects , So at this time JVM Will enter directly in the background “Stop the
World”： stop it Java All system worker threads , Let the code we write no longer run ! Then let GC Threads can execute safely GC：
This will shut down our system , Do not create new objects again , At the same time let GC The thread completes as soon as possible GC work ： Marking and transfer Eden and Survivor2 Live object to Survivor1, Then quickly recycle it all at once Eden and Survivor2 Garbage objects in ：
GC complete , You can resume Java The worker thread of the system runs , Continue in Eden create new object ：
<>Stop the World System pause caused by
GC To run 100ms, It may cause the system to stop directly 100ms, No requests can be processed . this 100ms During this period, all requests initiated by the user will be briefly stuck , Because the system worker is not running , The request cannot be processed . The assumption is Web system , May cause your users to download from the web page or APP Click a button , Usually only a few dozen ms You can return a response , Now because Web system JVM Executing Minor
GC, Pause all worker threads , Cause your request to come back to the response , This time it will take hundreds of minutes ms.
Because the memory allocation is unreasonable , This leads to the frequent entry of objects into the elderly generation , average 7,8min once Full GC, and Full GC Slowest , Sometimes it takes several times to recycle at a time s Even dozens s. At this time, once frequent Full
GC, How often does your system run min Dozens of people will get stuck s. This makes the user experience extremely poor . So whether the Cenozoic GC Or the old generation GC, Try not to be frequent , Also avoid prolonged periods .
<> Different effects of different garbage collectors
Cenozoic recycling ,Serial The garbage collector is used 1 Threads for garbage collection , Then pause the system worker at this time , Therefore, it is rarely used in general server programs . The commonly used new generation garbage collector is ParNew, It is generally multi-core for servers CPU Have optimization , Support multithreading GC, Significantly improve recycling performance , shorten GC time .
General principle ：
CMS It is also based on multithreading , And a unique mechanism can be used to reduce the waste in the process of garbage collection as much as possible “Stop the World” Time , Avoid system jamming for a long time .
G1 Moreover, a complex recycling mechanism will be adopted to optimize the recycling performance to the mechanism , Reduce as much as possible “Stop the
World” Time .JVM Self iterative evolution , Is to continuously optimize the garbage collector mechanism and algorithm , Minimize the impact of garbage collection process on system operation .
For those of us crud
boy, You have to optimize memory allocation and garbage collection for the threaded system , Minimize the frequency of garbage collection , Reduce garbage collection time , Reduce the impact of garbage collection on system operation .JVM Optimization is like this .