A major feature of the Java language is the ability to automate garbage collection without the developer focusing too much on system resources, such as the release of memory resources. Automatic garbage collection Although greatly reduce the developer's worries, basically do not worry about garbage collection problem, attention is basically oh, because we also have a common oom.
Owning a garbage collector can be said to be a significant difference between the Java language and the C + + language. In the C + + language, programmers must handle each memory allocation with care, and must manually release the memory space that was used after the memory is exhausted. When the memory is not fully released, a memory block that is allocated but never freed will cause a memory leak and, in severe cases, cause the program to crash.
Let's start by looking at which recycling algorithms are available. first, the recovery algorithm
Reference counting method (Reference counting)
Reference counters are used in Microsoft's COM component technology, Adobe's ACTIONSCRIPT3 species.
The implementation of the reference counter is simple, for an object A, as long as any one object refers to a, then the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. Object A can no longer be used as long as the value of the reference counter for object A is 0. The implementation of reference counters is also very simple, just to configure a single shaped counter for each object. However, the reference counter has a serious problem, that is, the case of a circular reference cannot be handled.
Therefore, this algorithm is not used in the Java garbage collector. A simple circular reference problem is described below:
There is object A and object B, and object A contains a reference to object B, and object B contains a reference to object A. At this point, the reference counters for object A and object B are not 0. However, there is no 3rd object in the system that references a or B. That is, a and B are garbage objects that should be recycled, but because of the mutual reference between the garbage objects, which makes the garbage collector unrecognized, causing a memory leak.
tag-purge algorithm (mark-sweep)
The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase.
A feasible implementation is to mark all large objects starting from the root node in the tagging phase first through the root node. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared. The biggest problem with this algorithm is that there is a lot of space debris, because the reclaimed space is discontinuous. In the process of object heap allocation, especially the memory allocation of large objects, the productivity of discontinuous memory space is lower than that of continuous space.
replication Algorithm (Copying)
Divide the existing memory space two times, use only one piece at a time, copy the surviving object in use in memory to the unused block of memory at garbage collection, then clear all objects in the memory block in use, swap the two memory roles, and complete the garbage collection.
If there are many garbage objects in the system, the number of surviving objects that the replication algorithm needs to replicate is not too large. As a result, the efficiency of the replication algorithm is high at a time when garbage collection is really needed. And because objects are uniformly copied to the new memory space during garbage collection, you can ensure that the reclaimed memory space is not fragmented. The disadvantage of this algorithm is to put the passbook in the system in half.
Java's new generation of serial garbage collector uses the idea of a copy algorithm. The Cenozoic is divided into 3 parts of Eden Space, from space, to space. The From space and to space can be considered as two blocks of the same size, equal status, and can be used for character interchange. The From and to spaces are also known as Survivor Spaces, which are survivor spaces that are used to store objects that have not been reclaimed. During garbage collection, the surviving objects in the Eden space are copied into unused survivor space (assuming to), and young objects in the survivor space being used (assuming from) are also copied into the to space (large objects, or older objects that go directly into the old age band, such as When the fruit to space is full, the object will go straight into the old age. At this point, the remaining objects in the Eden space and from space are garbage objects, which can be emptied directly, and the to space will hold the surviving objects after this collection. This improved replication algorithm not only guarantees the continuity of space, but also avoids a lot of wasted memory space.
tag-compression algorithm (mark-compact)
The efficiency of the replication algorithm is based on the premise that there are fewer surviving objects and more garbage objects. This happens often in younger generations, but more commonly in older times, most objects are living objects. If the replication algorithm is still used, the cost of replication will be high due to the large number of surviving objects.
Tag-compression algorithm is an old-age recovery algorithm, which has been optimized on the basis of the mark-sweep algorithm. It is also necessary to first mark all the objects that can be reached from the root node, but after that it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory. After that, clean up all the space outside the boundary. This method avoids the production of fragments, and does not require two blocks of the same memory space, therefore, it is more cost-effective.
Incremental Algorithm (Incremental collecting)
During the garbage collection process, the application software will be in a state of high CPU consumption. In this state of high CPU consumption, all threads of the application hang, pausing all normal work, waiting for the garbage collection to complete. If the garbage collection takes too long, the application will be suspended for a long time and will severely affect the user experience or the stability of the system.
The basic idea of the incremental algorithm is that if all of the garbage is processed at once, and the system needs to be paused for a long time, then the garbage collection thread and the application thread can be executed alternately. Each time, the garbage collection thread collects only a small area of memory space, and then switches to the application thread. Repeat until garbage collection is complete. In this way, due to the intermittent execution of the application code during the garbage collection process, the system's pause time can be reduced. However, because of the consumption of thread switching and context conversion, the overall cost of garbage collection increases, resulting in decreased system throughput.
Sub-generational (generational collecting)
According to the characteristics of garbage collection objects, the optimal way of different stages is to use the appropriate algorithm for garbage collection in this stage, the generational algorithm is based on this idea, it divides the memory interval according to the characteristics of the object, and uses different recovery algorithms to improve the efficiency of garbage collection according to the characteristics of each block. In the hot Spot virtual machine, for example, it puts all new objects into a memory area called the young generation, which is characterized by the fast recovery of objects, so that the younger generation chooses a more efficient replication algorithm. When an object is still alive after several recoveries, the object is put into a memory space called the Laosheng generation. In the Laosheng generation, almost all of the objects survived after several garbage collections. As a result, these objects can be thought of as resident memory for a period of time, even throughout the application's life cycle. If the Laosheng generation is still recycled using the replication algorithm, a large number of objects will need to be copied. Coupled with the Laosheng generation of recovery cost is lower than the new generation, so this practice is also undesirable. According to the idea of generational, the old age can be recycled using a different marker-compression algorithm to improve the efficiency of garbage collection. second, recycling strategy
The garbage collector can be divided into different types from different perspectives.
By the number of threads, it can be divided into serial garbage collector and parallel garbage collector. The serial garbage collector uses only one thread at a time for garbage collection, and the parallel garbage collector turns on multiple threads at the same time for garbage collection. Using a parallel garbage collector on a CPU with strong parallelism can shorten the pause time of GC.
According to the mode of operation, it can be divided into concurrent garbage collector and exclusive garbage collector. The concurrent garbage collector works alternately with the application thread to minimize application downtime; Once the exclusive garbage collector (Stop the World) runs, it stops all other threads in the application until the garbage collection process completely finishes.
Fragmentation can be divided into compressed garbage collector and non-compressed garbage collector. The compressed garbage collector will compress the surviving objects after the recycle is complete, eliminating the recovered fragments, and the non-compressed garbage collector does not do this step.
According to the working memory range, it can be divided into the new generation garbage collector and the old age garbage collector.
We can see that garbage collection actually has the following strategy: serial, parallel concurrent compression, non-compression, copy
You can use the following indicators to evaluate the quality of a garbage processor:
throughput : The ratio of the time spent by an application to the total elapsed time of the system during the lifetime of the application. Total system uptime = Application time +GC time consuming. If the system is running 100MIN,GC time consuming 1min, then the system throughput is (100-1)/100=99%.
Garbage collector load: In contrast to throughput, the garbage collector payload refers to the ratio of the time spent by the collector to the total system uptime.
Pause Time : The pause time of the application when the garbage collector is running. For an exclusive collector, the pause time may be longer. When using a concurrent collector, the program's pause time is shortened because the garbage collector and the application are running alternately, but the throughput of the system may be lower because it is probably less efficient than an exclusive garbage collector.
garbage Collection Frequency : Refers to how long the garbage collector runs. In general, for fixed applications, the garbage collector should be as low as possible. Generally, increasing heap space can effectively reduce the frequency of garbage collection, but it may increase the amount of downtime that is generated by recycling.
reaction time : The amount of memory space that is occupied by an object when it is called Garbage is released.
heap allocation : Different garbage collector allocations to heap memory may be different. A good garbage collector should have a reasonable breakdown of heap memory intervals.
With some recycling algorithms and strategies, plus some evaluation metrics, we can look at the garbage collector. third, Garbage collector
Serial collector
Pros: Simple and efficient (the highest single-threaded garbage collection efficiency can be achieved with no thread allocation overhead compared to other collectors running in a single CPU environment)
The new generation collector, using the copy algorithm, single-threaded, virtual machine-client mode under the new generation of the default collector.
Single-threaded garbage collection, and when it is garbage collected, all other worker threads must be suspended until the end of garbage collection, the process (stop the World) automatically initiated by the virtual machine in the background auto-completion, the user will not be visible under the conditions of the user all the normal work threads to stop all. Of course, if the pause time is very short is acceptable, but if each pause for 5 minutes, everyone will crash.
For some client programs, the new generation occupies a small amount of memory space, where the pause time can be fully controlled at the millisecond level, so the serial collector is a good choice for virtual machines running in client mode.
parnew Collector
Advantage: Optimize multi-CPU usage
New generation collectors, using replication algorithms, multithreading.
The multi-threaded version of the serial collector, in addition to using multithreading for garbage collection, the rest of the behavior is exactly the same as the serial collector, and will still stop the world.
Many of the preferred Cenozoic collectors in virtual machine-server mode are due to the fact that the CMS collector (old age collector) can only be used in conjunction with the serial collector or parnew collector.
The number of recycle threads that are turned on by default is the same as the number of CPUs (modern servers with 32 logical CPUs will cause the Parnew collector to turn on 32 collection threads, in which case it is best to limit the number of collection threads).
Common configuration:
-xx:parallelgcthreads Multi-threaded garbage collector memory Reclaim the number of threads that are turned on.
Parallel Scavenge collector
Benefits: Increased Application throughput
New generation collectors, using replication algorithms, multithreading.
The collector's focus is different from that of other collectors, and the goal of the other collector is to minimize the downtime of the user thread during garbage collection, which is aimed at achieving a manageable throughput of throughput = Running user code time/(running user code time + garbage collection time).
Short pause time is fast response, suitable for interacting with users more applications; throughput is the most efficient use of CPU time, as soon as possible to complete computing tasks, suitable for background operations, not many applications.
Common configuration:
-xx:maxgcpausemillis Maximum pause time, only valid for parallel scavenge collectors.
-xx:gctimeratio throughput size, the default value is 99, which is 1% GC time, which is only valid for parallel scavenge collectors.
-xx:+useadaptivesizepolicy uses the GC adaptive throttling policy, if you enable this policy, you only need to set the basic parameters (-XMX, etc.), and then set an optimization target (maximum pause time or throughput size), The virtual opportunity collects performance monitoring information based on the health of the current system, dynamically adjusts the detail parameter settings (for example: the size ratio of Eden and survivor areas in the-xx:survivorratio Cenozoic, the default is 8, which is Eden:survivor=8:1,-xx: Pretenuresizethreshold direct Promotion old age object size, objects larger than this size will be allocated directly in the old age, only serial and parnew collectors know this parameter,-xx:maxtenuringthreshold Promotion old age object age, each object after a minor GC object age +1, more than the set value of the object moved to the old age) to provide the most appropriate pause time and maximum throughput.
Serial Old collector
The old age collector, using the tag-sorting algorithm, single-threaded, serial-collector version of the old age.
As a fallback collector for the CMS collector: When the CMS collector generates concurrent Mode failure, the serial old collector is temporarily restarted for garbage collection in the older age.
Parallel Old collector
The old age collector, using the tag-collation algorithm, multi-threading, Parallel scavenge collector of the old age version.
The Parallel scavenge collector + Parallel old collector is a priority in situations where throughput or CPU resource sensitivity is a concern.
CMS (Concurrent Mark Sweep) collector
Advantages: Fast recovery, short pause
Old age collector, using tag-purge algorithm, multithreading concurrency.
In order to achieve the shortest garbage collection quite a pause time for the target collector.
The collector's collection process is divided into 4 steps:
1, initial tag CMS initial mark: This step will stop the world, but it takes a very short time to mark the objects directly associated with GC root.
2, concurrent tagging CMS concurrent mark: Takes a long time, the user thread can run at the same time, marking the GC root with an object that can reach the path.
3, re-tagging 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 to change the object tag resulting from the user thread synchronization run in step 2.
4, concurrent purge CMS concurrent sweep: Takes a long time and the user thread can run concurrently.
Both the user thread and the collection thread can work concurrently during the lengthy concurrency tagging phase and the concurrent purge phase, and the memory reclamation of the CMS collector is, in general, executed concurrently with the user thread.
Generally, most Web applications now use the Parnew+cms collector (jdk1.6/1.7) by default.
Disadvantages:
1, the CPU resource-sensitive, the CMS collector by default on the number of collection threads (CPU number +3)/4, if the number of CPUs is small, will consume a lot of CPU processing resources.
2, cannot handle floating garbage, and may produce concurrent Mode failure thereby causing 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 is generated after the marking process, the GC has not been marked after the purge, can only be left to the next GC processing, known as floating garbage.
Due to the execution of the collection thread of the CMS, the user thread will also execute simultaneously, resulting in the CMS collector not being able to perform GC when memory is almost exhausted in the old age collector, and must reserve some memory for the user thread (the default value for the low version JDK is 68%,JDK6 and the previous version defaults to 92% ), if the reserved memory fails to meet the user thread's execution, concurrent Mode Failure will appear, at which point the virtual machine will start the fallback scheme and call the serial old collector to perform a full GC, which will result in a longer collection pause.
3, memory fragmentation due to the implementation of the tag-purge algorithm (the disadvantage of the mark-sweep algorithm)
Configuration:
-xx:cmsinitiatingoccupancyfraction set the percentage of GC firings, too high will cause excessive concurrent Mode Failure, too low to affect performance, only for the CMS collector to take effect.
-xx:+usecmscompactatfullcollection provides memory grooming after full GC, which is not concurrent and can result in degraded performance and is only valid for the CMS collector.
-xx:cmsfullgcsbeforecompaction set a full GC with compression done several times without compression, and only for the CMS collector.
G1 (Garbage first) collector
Benefits: CMS Optimization
The G1 collector does not use the traditional physical isolation of the Cenozoic and the old (only logically dividing the Cenozoic and the old), but instead divides the entire heap memory into 2048 separate region blocks of the same size, each of which depends on the actual size of the heap, Overall is controlled between 1m-32m, the G1 collector tracks the garbage accumulation in the region and maintains a priority list in the background, recovering the highest priority area per set garbage collection time, which avoids garbage collection throughout the next generation or throughout the old age, making the Stop the World is shorter, more controllable, and has the highest recovery efficiency in a limited amount of time.
Configuration:
-xx:g1reservepercent set the reserved percentage of free space to reduce the risk of space overflow, with a default value of 10, or 10%
The collector uses the configuration:
-XX:+USESERIALGC collector combination using serial+serial old
-XX:+USEPARNEWGC collector combination using parnew+serial old
-XX:+USECONCMARKSWEEPGC collector combination using parnew+cms/serial old (Serial-old standby)
-XX:+USEPARALLELGC collector combination using parallel scavenge+serial old
-XX:+USEPARALLELOLDGC collector combination using Parallel scavenge+parallel old
-XX:+USEG1GC using a G1 collector for memory recycling
In general, it is recommended that you use the default garbage collector in cases where you do not have an explicit purpose to adjust the scenario.
Original address: https://www.ibm.com/developerworks/cn/java/j-lo-JVMGarbageCollection/
Http://www.360doc.com/content /13/0313/22/11098634_271354611.shtml
Text is slightly censored