Performance optimization|The most easy-to-understand explanation of 5 garbage collectors in history, exclusive arrangement

The garbage collector is a concrete implementation of the garbage collection algorithm

Serial collector (-XX:+UseSerialGC -XX:+UseSerialOldGC)

Under the literal meaning, the Serial collector is a string collector, which performs garbage collection in a single thread, that is to say, in a multi-core CPU, it still performs garbage collection in a single thread;
Insert picture description here

When the Serial collector is working, it will stop all running code, and STW (stop the world) will appear, and the code will not resume until the end of the collection.

The Serial collector runs in a single thread and does not need to bear the additional loss of thread switching, thereby improving collection efficiency.

Serial has two purposes. The first is to use with Parallel Scavenge in jdk1.5 and before, and the other is to use with the CMS collector. If gc fails, it will use the Serial collector to collect garbage in the old age.

ParNew collector (-XX:+UseParNewGC)

The ParNew collector is a multi-threaded version of the serial collector. STW will also appear during the collection process. The number of threads recycled by default is the same as the number of CPU cores in the system. Of course, you can also use the -XX:ParallelGCThreads parameter to specify recycling. Number of threads
Insert picture description here

Parallel Scavenge collector (-XX:+UseParallelGC (young generation),-XX:+UseParallelOldGC (old generation))

Parallel Scavenge collector is similar to ParNew collector, it is the default collector in Server mode (memory greater than 2G, 2 cpu).

The Parallel Scavenge collector focuses on throughput, which refers to the ratio of the time consumed by the user to run the code and the total time consumed by the CPU, and will also lead to the STW phenomenon. Parallel old is the collector of the old age.

CMS collector (-XX:+UseConcMarkSweepGC(old))

The CMS collector is a true concurrent garbage collector, which allows user programs and garbage collectors to work at the same time.

The CMS collector is different from the previous collectors. In order to achieve concurrent collection and to collect more cleanly, it adds multiple steps in the collection process:

  • Initial mark: Pause all threads, only record objects that gc root can directly reference, which is very fast;
  • Concurrent marking: At this stage, the user program is not stopped, the collector and the user program run at the same time, and the reachable objects are marked;
  • Re-marking: This stage is used to mark those newly generated reachable reference objects in the concurrent marking stage. This stage will suspend the user thread, causing the user thread to temporarily pause
  • Concurrent cleanup: At this stage, the GC thread and user thread run at the same time, and the GC thread starts to clean up those unreachable objects that have not been marked
  • Concurrent compression: If the user turns on the -XX:+UseCMSCompactAtFullCollection parameter, fragmentation processing will be performed;
  • Concurrent reset: After cleaning, the flag bits of those marked objects will be cleared;
    Insert picture description here

The advantages and disadvantages of the CMS collector:

advantage

  • Short pause time
  • Fast collection (using concurrent collection)

Disadvantage

  • After collection, fragments will be generated (you can use -XX:+UseCMSCompactAtFullCollection to compress after cleaning)
  • Run while collecting, resulting in unclean collection
  • During the collection process, the program is running, and the collection may not be completed yet. If a FULL GC is triggered, "concurrent mode failure" will appear. If this occurs, the CMS collector will be degraded. Use the serial collector (Serial old), garbage collection is performed, STW will still appear at this time.

G1 collector (-XX:+UseG1GC)

The g1 collector is a garbage collector developed for servers with large memory and multi-processors. It can provide low-pause collection time and high throughput.

The g1 garbage collector is different from all the above garbage collections. It will divide the space into regions of the same size. The JVM can have up to 2048 regions. The region size can be specified by the parameter -XX:G1HeapRegionSize;

By default, the size of the young generation accounts for 5% of the heap space, which can be modified with the parameter -XX:G1NewSizePercent;

Different from other garbage collectors, there is an additional Humongous area in the g1 garbage collection. If the memory size allocated by an object exceeds 50% of the region area, it will be directly placed in the Humongous area. If the object is too large, a region If you can’t put it down, you can store across multiple regions. The purpose of this is to prevent short-lived large objects from being allocated to the old generation, resulting in insufficient space in the old generation and frequent full gc;

The recovery steps of the g1 collector:

  • Initial mark: pause all threads and mark objects that gc root can directly reference;
  • Concurrent mark: same as CMS mark;
  • Final marking: same as CMS re-marking;
  • Screening and recycling: g1 will analyze the recycling cost of each region. The recycling cost refers to how long it takes to recover the non-surviving objects in this region; after completing this analysis, it will be sorted, which is low cost Start recycling to a high place, but not all at once. The specific recycling amount is set by the GC pause time parameter (-XX:MaxGCPauseMillis) expected by the user. If the user expects the GC pause time to be 200ms, then the jvm is before the recycling It will be calculated, and the total pause time of the several regions is just within 200ms, and the satisfied regions are recovered, so as to control the pause time of GC.

The main feature of g1 collector: predictable pause;

Classification of g1 collectors

YoungGC

The young gc collected by g1 is different from the events triggered by the young gc of other collectors. If the eden area is full, the young gc will not be triggered once, and g1 will calculate whether the time to reclaim all the current eden areas is greater than or equal to the -XX:MaxGCPauseMills parameter If the set value is less than, a new region will be created as the eden area for the new object to store, otherwise young gc will occur;

MixedGC

This is a newly added gc category in g1. It will reach the value set by the parameter (-XX:InitiatingHeapOccupancyPercen) in the old age area, and it will trigger. The area it reclaims is the young area and part of the old area. Use Copy algorithm, copy live objects to other empty regions, if there is not enough memory, it will trigger full gc

FULL gc

Stop all threads, use a single thread to collect all garbage, use mark-clean-compress;

Search on WeChat [AI Coder] Follow the handsome me and reply [Receive dry goods], there will be a lot of interview materials and architect must-read books waiting for you to choose, including java basics, java concurrency, microservices, middleware, etc. More information is waiting for you.

Guess you like

Origin blog.csdn.net/weixin_34311210/article/details/109482058