The current commercial virtual machine garbage collection uses the "generational collection" algorithm, that is, according to the different life cycle of the object, divided into several pieces of memory, generally for the Cenozoic and the old era, different generations according to its characteristics of the use of the most appropriate garbage collection algorithm
one, tag-purge algorithm
The algorithm is divided into "mark", "clear" 2 process, first mark the object that needs to be cleared, then unify clear
There are 2 obvious drawbacks to this algorithm:
1, the efficiency of marking and clearance is not high
2, after garbage collection, memory fragmentation is serious
A modified algorithm is usually used in old age
two, mark-finishing algorithm
Same as the tag-erase algorithm, and then let all the surviving objects move to one end, and finally clear out the parts outside the bounds
There is no memory fragmentation problem compared to the tag-purge algorithm, which is commonly used in old age
third, the replication algorithm
2 blocks of the same size of memory, using only one piece at a time. When a piece of memory consumes light, the object that is still alive is copied to another piece, and the original piece is emptied
The advantages and disadvantages of this algorithm are more obvious
The advantage is that the implementation is simpler, the efficiency is high, and there is no memory fragmentation problem
The disadvantage is that the maximum available memory becomes the original One-second, which is not to be expected.
The new generation is characterized by a very small number of objects that are still alive after each garbage collection. If this algorithm is used for the Cenozoic, there is no need to retain one-second of the available memory
You can divide the memory into a larger Eden space and 2 smaller survivor spaces in the 8:1:1 ratio, each time using only the Eden space and a survivor space, 90% of the memory usage is acceptable compared to the original 50% memory usage rate.
When memory is reclaimed, the objects that are still alive are copied to another survivor space, and the original survivor Space and Eden Space are emptied and the efficiency is very high.
According to the characteristics of the Cenozoic, there are few objects that still exist, but what to do if a survivor space cannot provide enough space for the surviving object to be stored. The answer is to go to the old age to rent a little memory for emergency
In some extreme cases, the target survival rate may be 100%, when more memory needs to be requested from the old age
One, serial collector
Cenozoic collectors, using the replication algorithm
Single-Thread collector
Virtual machine-client mode new generation default collector
Single thread for garbage collection, and when it is garbage collected, all other worker threads must be paused until the garbage collection is finished, and the process (stop the World) automatically initiates automatic completion in the background by the virtual machine, which stops all normal worker threads of the user under conditions that are not visible to the user. Of course, it's acceptable to pause for a short period of time, but if you pause for 5 minutes, anyone will collapse.
For some client programs, the new generation consumes a small amount of memory, which can be controlled at the millisecond level at all times, so the serial collector is a good choice for virtual machines running in client mode.
Advantages: Simple and efficient (relative to other collectors running in a single CPU environment.) No thread allocation overhead to achieve the highest level of single thread garbage collection efficiency
Two, parnew collector
Cenozoic collectors, using the replication algorithm
Multithreading Collector
The multi-threaded version of the serial collector, in addition to using multithreading for garbage collection, is the same as the rest of the serial collector, which will still stop the world
Many virtual machines are the preferred generation collector in-server mode, mainly because the CMS collector (old age collector) can only be used in conjunction with the serial collector or the Parnew collector
The number of recycled threads opened by default is the same as the number of CPUs (modern server 32 logical CPUs will cause the Parnew collector to open 32 collection threads, in which case it is best to limit the number of threads to be collected)
-xx:parallelgcthreads number of threads open for multithreaded garbage collector memory Recycle
Three, Parallel scavenge collector
Cenozoic collectors, using the replication algorithm
Multithreading Collector
The collector's concerns are different from those of other collectors, the goal of other collectors is to minimize the downtime of the user's thread when garbage collection is achieved, the target of which is to achieve a controllable throughput, throughput = Run user code time/(run user code time + garbage Collection Time)
Short pause time is fast response, suitable for interaction with the user more applications, throughput can be the most efficient use of CPU time, as soon as possible to complete the operation task, suitable for background operation, not much interaction application
-xx:maxgcpausemillis Maximum pause time, only for parallel scavenge collector
-xx:gctimeratio throughput size with default value of 99, or 1% GC time, only effective for parallel scavenge collector
-xx:+useadaptivesizepolicy uses the GC adaptive throttling policy, if you enable this policy, you only need to set the basic parameters (-XMX, and so on), and then set an optimization target (maximum pause time or throughput size). Virtual Opportunity collects performance monitoring information based on current system's health, dynamically adjusts details parameter settings (for example:-xx:survivorratio the ratio of the new generation to the Survivor area, the default is 8, namely Eden:survivor=8:1,-XX: Pretenuresizethreshold direct promotion of old age object size, objects larger than this size will be directly allocated in the old age, only serial and parnew collectors recognize this parameter,-xx:maxtenuringthreshold Promotion of old age object age, each object after a minor GC object age +1, over setting a numeric object to move to the old age to provide the most appropriate pause time and maximum throughput
Four, serial old collector
Old age collector, using tag-collation algorithm
Single-Thread collector
Old age version of the serial collector
Fallback collector as a CMS collector: When the CMS collector generates concurrent Mode failure, the temporary boot of the serial old collector is used to recreate the garbage collection of the older age
Five, Parallel old collector
Old age collector, using tag-collation algorithm
Multithreading Collector
JDK1.6 offers, old age versions of Parallel scavenge collectors
Priority is given to Parallel scavenge collectors + Parallel old collectors in situations that focus on throughput or CPU resource sensitivity
Six, CMS (Concurrent Mark Sweep) collector
Old age collector, using tag-purge algorithm
Multithreading Concurrency Collector
Collector with the shortest garbage collection and pause time as the target
The collector's collection process is divided into 4 steps:
1, initial labeled CMS initial mark: This step will stop the world, but it takes a very short time to mark objects directly associated with GC root
2, concurrent labeled CMS concurrent mark: Long time, user thread can run concurrently, Mark to the GC root has a path to the object
3, re-mark CMS remark: This step will stop the world, but it takes a very short time. Because the user thread in step 2 runs synchronously, the main fix is the OBJECT tag changes that are generated by the user thread running synchronously in step 2
4, concurrent clear CMS concurrent sweep: Time-consuming, user threads can run simultaneously
Both the user thread and the collection thread can work simultaneously during the lengthy concurrent markup phase and the concurrency cleanup phase, so the memory reclamation of the CMS collector is generally performed concurrently with the user thread
Although the collection pause time is short, but also has many disadvantages:
1, sensitive to CPU resources, the CMS collector by default to open the number of collection threads (CPU number +3)/4, if the number of CPUs is small, will occupy a lot of CPU processing resources
2, unable to process floating garbage, and may produce concurrent Mode failure resulting in another full GC.
Concurrent Purge (step 4), the user thread can be run at the same time, the user thread will generate new garbage, this part of the garbage after the marking process, the GC has been unable to mark the purge, can only be left until the next GC processing, known as floating garbage
As the collection thread for the CMS executes, user threads are also executed at the same time, causing the CMS collector to not perform the GC as it did in the old years as other old-age collectors do, and must reserve part of the memory for the user thread (the lower JDK default is 68%,JDK6 and the default value of version 92% If the reserved memory does not meet the user thread's execution, a concurrent Mode failure will appear, at which point the virtual machine will start the standby and invoke the serial old collector to perform a full GC, which will result in a longer collection pause
-xx:cmsinitiatingoccupancyfraction set the percentage of GC triggers, too high can cause too much concurrent Mode failure, too low to affect performance, only the CMS collector takes effect
3, as a result of the tag-purge algorithm, memory fragmentation is generated
-xx:+usecmscompactatfullcollection The full GC provides a memory collation that cannot be concurrent, resulting in degraded performance and only takes effect on the CMS collector
-xx:cmsfullgcsbeforecompaction set a full GC with a compressed full after a few full GC without compression, only for the CMS collector
Seven, G1 (garbage) collector
JDK provides this collector starting from version 1.7
Instead of using the traditional layout of the Cenozoic and old age physical isolation (only logically dividing the Cenozoic and the old), the G1 collector divides the entire heap memory into 2048 independent region blocks of the same size, each of which depends on the actual size of the heap, The whole is controlled between 1m-32m, the G1 collector tracks the garbage accumulation in region and maintains a priority list in the background, each time the highest priority area is recycled based on the set garbage collection time, which avoids garbage collection throughout the new generation or throughout the old age, making it possible to stop the The world is shorter and more controllable, and can achieve the highest recovery efficiency in a limited amount of time
-xx:g1reservepercent sets the percentage of free space reserved to reduce the risk of space overflow, with a default value of 10, or 10%
Combined use of collectors:
-XX:+USESERIALGC Use the collector combination of serial+serial old
-XX:+USEPARNEWGC Use the collector combination of parnew+serial old
-XX:+USECONCMARKSWEEPGC uses a collector combination of parnew+cms/serial old (serial old as standby)
-XX:+USEPARALLELGC Use the collector combination of parallel scavenge+serial old
-XX:+USEPARALLELOLDGC Use the collector combination of Parallel scavenge+parallel old
-XX:+USEG1GC using the G1 collector for memory recycling
Ps:
Minor GC: Occurring in the Cenozoic GC, very frequent, fast
Full Gc/major GC: GC that occurs in the old age, typically 10 times times slower than minor