Master three garbage algorithms, seven garbage collectors, and learn what garbage collection algorithms are used for each garbage collector, as well as knowledge points about SafePoint.
garbage Collection Algorithm1. Tag-purge algorithm (mark-sweep)
tags (such as the accessibility algorithm) are all objects that need to be recycled, and all tagged objects are reclaimed after the tag is marked.
Disadvantage: The labeling and purging process is inefficient and can result in a large amount of discontinuous memory fragmentation.
2. Copy Algorithm (Copying)
Divide the available memory into two blocks of equal size, using only one piece at a time. When one piece of memory is used up, the surviving object is copied to another piece of memory, and then the used memory space is cleaned out once.
The principle of Cenozoic recovery is to use the replication algorithm, the hotspot virtual machine will be divided into 1 Eden and 2 survivor, the default ratio is Eden:Survior1:Survior2 = 8:1:1. When recovering, the objects that are still alive in Eden and Survior1 are copied to Survior2 at once, and then the space of Eden and Survior1 is cleared out. Since there is no way to guarantee that the surviving object after each recovery is less than 10%, it is necessary to assign a guarantee to the new generation that if another piece of survivor does not have space for the last generation of surviving objects collected, these objects will enter the old age directly through the allocation of security mechanisms.
Advantages and Disadvantages: Although the replication algorithm does not consider the problem of memory fragmentation, simple and efficient, but also need to waste a portion of space, there is a need to consider the allocation of security, suitable for the new generation of low-survival object.
3. Labeling-Sorting algorithm (mark-compact)
For older generations with higher object survival, it is not appropriate to use a replication algorithm because more replication is needed and additional space is required for allocation guarantees.
In the old age, the "mark-and-sweep" algorithm was used to mark the objects that needed to be recycled, then all the surviving objects moved to one end, and then all the memory outside the end boundary was cleaned up directly.
4, Generational collection algorithm (generational Collection)
According to the appropriate collection algorithm in the different ages of Java heap, 98% of the objects in the Cenozoic are dying in the new generation, which is suitable for the replication algorithm, only a small number of surviving objects need to be copied. In the old age, the object has a high survival rate, and there is no additional space to vouch for it, which is appropriate for the "mark-clear" or "mark-sweep" algorithm.
garbage collector
The following 7 types of collectors are used in different generations, and there is a connection between the two collectors, indicating that they can be combined.
1. Serial Collector
The new generation collector, single-threaded, GC pauses all other worker threads (stop the world), using the copy algorithm.
Virtual machine in the client mode of the default new generation collector, in a single CPU environment, serial collector due to no thread interaction overhead, simple and efficient.
Turn on the serial collector:-XX:+USESERIALGC, run with serial old.
2. Parnew collector
The new generation collector, multi-threaded parallel, GC pauses all other worker threads (stop the world), using the copy algorithm, is a multithreaded version of the serial collector.
Many of the new generation collectors are preferred by virtual machines in server mode because only the serial and parnew collectors can work with the CMS (old age) . The default Cenozoic collector after selecting CMS (-XX:+USECONCMARKSWEEPGC) is parnew and can also be specified as Parnew collector by the-XX:+USEPARNEWGC command.
If it is a single CPU environment, serial will have a better effect, but as the number of CPUs increases, parnew is more appropriate. Parnew the number of GC threads opened by default is the same as the number of CPUs, the number of GC threads can be limited by-xx:parallelgcthreads.
Parallel: Multiple GC threads work in parallel, but at this point the user thread is still waiting, that is, the GC thread and the user thread cannot work at the same time, one of the threads is working, and the other thread needs to be stopped, and a CPU executes only one thread.
Concurrency: The user thread executes concurrently with the GC thread (not necessarily in parallel, may be alternately executed), the user program continues to run, and the garbage collector runs on the other CPU. At the point of view of a CPU, it is generally run concurrently (alternating execution)
The running process used with serial old is as follows.
3. Serial Old collector
The old version of serial, single threaded, uses "mark-organize".
Typically used for virtual machines in client mode, if in server mode one purpose is to use JDK 1.6 with the parallel scavenge collector, another use is as a CMS A backup plan for the collector .
4. Parallel Scavenge collector
New Generation collector, multithreading parallel, using replication algorithm.
It looks similar to the Parnew collector, but the difference is that parallel scavenge's focus is on the ability to control throughput . The focus of the CMS is to shorten the GC when the user thread pause time, low pause time for the need to interact with the user of the program, high throughput can be efficient use of CPU time, as soon as possible to complete the operation of the program tasks, mainly for the background operation and do not need too many interactive tasks. Throughput is the ratio of the amount of time the CPU spends running user code to the total CPU time, that is, throughput = Running user code time/(running user code time + garbage collection time), such as virtual machines running for a total of 100 minutes, where garbage collection takes 1 minutes, and that throughput is 99%.
Xx:maxgcpausemillis: Control the maximum garbage collection pause time, more than 0 milliseconds, the collector will try to ensure that GC time does not exceed the set value, but is not set up the GC is fast, the settings are small, the GC is frequent, GC pause time is reduced at the expense of throughput and new generation of space to exchange, the new generation of smaller, the collection of 300MB generation is certainly faster than the collection of 500MB, but this also directly lead to garbage collection occurs more frequently, the original 10 seconds collected once, each pause 100 milliseconds, now becomes 5 seconds to collect, 70 milliseconds per pause. The pause time is indeed falling, but the throughput is lowered.
-xx:gctimeratio: Directly set the throughput size, greater than 0 and less than 100 integer, the reciprocal of the throughput, if set to 19, the maximum GC time allowed for the total time of 5% (that is, 1/(1+19)), the default value is 99, which is allowed maximum 1% (that is, 1/(1+99)) Garbage collection time.
-XX:+USEADAPTIVESIZEPOLICY:GC Adaptive Adjustment Strategy, virtual opportunity to collect performance monitoring information according to the current system operation, dynamically adjust the Cenozoic size (-xmn), Eden and Survivor area ratio (-XX: Survivorratio), the promotion of the old Age Object Age (-xx:pretenuresizethreshold) and other detail parameters to provide the most appropriate pause time or maximum throughput. Just set the basic memory data (such as-XMX to set the maximum heap), then use Maxgcpausemillis (more attention to maximum pause time) or gctimeratio (more attention to throughput) to set up an optimization target for the virtual machine, and then the specific details of the parameters by the virtual machine adaptive adjustment.
The running process used with parallel old is as follows.
5. Parallel Old collector
Parallel scavenge's old version, multi-threading, "tag-collation" algorithm.
Before JDK1.6, the Parallel scavenge can only be combined with the old age collector serial (PS MarkSweep), which can slow down the overall performance due to the inability of the serial to take full advantage of the server's multi-CPU processing power.
After JDK1.6, Parallel scavenge can be combined with Parallel old to achieve a veritable "throughput priority", which can be prioritized in the context of throughput-sensitive and CPU-intensive situations.
6. CMS collector (Concurrent Mark Sweep)
Based on the "mark-sweep" algorithm, low pauses, concurrent collection. The goal is to obtain the shortest recovery pause time and low latency, which is suitable for applications that pay attention to service response speed.
The main process is the following 4 steps--
(1) Initial mark: will go through Stop the world, mark the GC roots can directly relate to the object, fast.
(2) Concurrency token: GC Roots tracing, from GC Roots for accessibility analysis, search for the associated reference chain, which can be performed concurrently with the user thread.
(3) Re-tagging: Also need stop the world, because the concurrent tagging phase program is still running, may cause some changes in the markup, this phase is to fix the concurrency tag during the operation of the user program continues to change the part of the mark, the pause time is slightly longer than the initial tag, but much shorter than the concurrent tag.
(4) Concurrent cleanup: GC Threads and user threads can be concurrently.
CMS running process as.
Because the most time-consuming concurrency token and concurrent cleanup process GC threads can work with the user thread, the CMS is performed concurrently with the user thread as a whole.
3 Disadvantages of CMS--
(1) The CPU resource is very sensitive, the CMS default startup recycle thread number is (CPU number +3)/4, if the number of CPUs is good, but if the number of CPUs less than 4, GC will occupy nearly half of the CPU resources, if the CPU load is relatively large, Also, half of the computational power to execute the collector thread may result in a sudden reduction in the execution speed of the user program, which consumes too much CPU resources during GC to reduce the total throughput.
(2) Unable to handle floating garbage, because in the concurrent cleanup phase, the user thread will also run, so the new garbage generated, and the new garbage is generated after the tag, the garbage is called floating garbage, CMS can only wait until the next GC to process them.
Because the garbage collection phase user thread also needs to run, so it is necessary to set aside enough memory for the user thread, CMS can not wait for the old age to be almost completely filled up and collected, JDK 1.6, the old age of 92% space will activate the CMS collector, through-XX: Cmsinitiatingoccupancyfraction can set the trigger scale, if the memory reserved during the CMS operation can not meet the needs of the program, there will be a "Concurrent Mode Failure", At this point the virtual opportunity temporarily enables the serial old collector to re-age garbage collection, the pause time is very long. So-xx:cmsinitiatingoccupancyfraction setting too high easily leads to a lot of "Concurrent Mode Failure", too low frequent GC can also affect performance.
(3) A large amount of space debris is generated because the CMS collector is based on the "tag-purge" algorithm, so there is a lot of space fragmentation at the end of the collection, and the following two parameters can be adjusted for defragmentation.
-xx:+usecmscompactatfullcollection: By default, used to turn on memory fragmentation when the CMS collector is not up to the full GC, the process of memory consolidation is not concurrent, the space debris problem is not, but the pause time must not be longer.
-xx:cmsfullgcsbeforecompaction: Used to set how many times the uncompressed full GC is followed by a compressed one (the default value is 0, which means defragmenting every time you enter the full GC).
7. G1 Collector
The Java heap layout of the G1 collector and other collectors is very different, the collection of the other collectors is the entire new generation or the old age, while G1 divides the entire Java heap into multiple independent regions of equal size, the Cenozoic and the old age are no longer physically isolated. The spatial distribution is as shown.
G1 still uses the generational algorithm , still copy the surviving objects to the old age or survivor space, the old age is also divided into many areas, G1 collector by the object from one region to another area, the completion of the cleanup work, This will not have the existence of a CMS memory fragmentation problem. The area of H in the figure represents humongous, which means that these regions store large objects (humongous object,h-obj), that is, objects larger than half the size of region, H-obj are assigned directly to old Gen, preventing repeated copy movement, If a large object is not installed in an H-zone, then G1 will look for contiguous H-partitions to be stored, sometimes having to start full GC in order to find contiguous H-zones.
G1 In addition to the pursuit of low pauses, there is another feature, can establish a predictable pause time model , can let the user explicitly specified in a time fragment length of M milliseconds, consumed in garbage collection time not more than n milliseconds, This is because G1 tracks the value of garbage accumulation in each region, maintains a prioritized list in the background, and prioritizes recovery of the most valuable region (the origin of the G1 name) each time it is allowed to collect.
The next question is the question that other collectors will face: Are there possible referential relationships between arbitrary objects in the entire Java heap, and do you want to scan the entire Java heap when the accessibility decision determines whether the object is alive or not?
G1 and other collectors are remembered set to avoid full heap scanning : Each region in the G1 has a corresponding remembered set, and when the Virtual machine Discovery program writes to the reference type of data, Temporarily interrupts the write operation to check if the object referenced by reference is in a different region (generational checks if objects in the old age refer to objects in the Cenozoic), and if so, the reference information is recorded in the remembered set of the region to which the referenced object belongs. When memory reclamation is in progress, adding the remembered set to the enumeration scope of the GC root node guarantees that no full heap scan will be missed.
The G1 is a garbage collector for service-side applications that aims to replace the CMS in the future.
Removing the maintenance of remembered set, the G1 operation process is broadly divided into 4 steps:
Initial tag: marks the object that GC roots can directly relate to, briefly pauses the thread.
Concurrency token: From GC root, you can analyze the objects in the heap for accessibility, find the surviving objects, take a long time, and can be concurrent with the user thread.
Final tag: Fixed the part of the tag record that generated changes during the concurrency tag, the virtual opportunity to record the change of this time object into the remembered set logs of the thread, and finally merge the data of remembered set logs into the remembered set. Requires a stalled thread, which can be parallel.
Filter Recycling: The recovery value and cost of each region are sorted first, according to the desired GC pause time to develop a recycling plan, can be concurrent with the user thread, this phase only a portion of the region, the time is user-controllable.
The G1 collector run diagram is as follows.
Combined above, G1 has the following characteristics:
* Parallel and concurrent: G1 can take full advantage of multiple CPUs to shorten the Stop-the-world pause time, G1 collectors can still be in a concurrent way to let Java programs continue to execute during GC.
* Generational collection: As with other collectors, generational concepts remain in G1, and G1 can independently manage the entire GC heap without the need for additional collector mates.
* Spatial Integration: Unlike the CMS's "mark-and-clean" algorithm, G1 is based on the overall "mark-and-sweep" algorithm implementation of the Collector, from the local (two region) is based on the "copy" algorithm is implemented, but anyway, Both of these algorithms mean that there is no memory space fragmentation during the G1 operation and that the collected data will provide regular, usable memory. This feature is useful for long-running applications where large objects are allocated without triggering the next GC ahead of time because of the inability to find contiguous memory space.
* Predictable pauses: Reducing the pause time is a common concern for G1 and CMS, but the G1 can also build a predictable pause-time model in addition to the pursuit of low pauses.
G1 compared with parallel scavenge/ps old, the greatest benefit is that the pause time is more controllable and predictable. If the pursuit of low pauses, you can try G1, if the pursuit of throughput, G1 will not have special benefits.
G1 parameter configuration:
-XX:+USEG1GC: Specifies the use of the G1 collector;
-xx:initiatingheapoccupancypercent: When the overall Java heap occupancy rate reaches the parameter value, the concurrency tagging phase is started; the default is 45;
-xx:maxgcpausemillis: Set pause time target for G1, default value is 200 milliseconds;
-xx:g1heapregionsize: Sets the size of each region, ranging from 1MB to 32MB, with a target of about 2048 areas in the smallest Java heap;
Reference Links:
53983650
51050824
Http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/g1_gc_tuning.html#important_defaults
Https://www.cnblogs.com/ASPNET2008/p/6496481.html
Https://www.cnblogs.com/oldtrafford/p/6883796.html
73743395
Https://www.cnblogs.com/woshimrf/p/jvm-garbage.html
Enumerate root nodes, security points, security zones
After summarizing the garbage collection algorithm and the garbage collector, there are several small details.
1. Enumeration of root nodes, security points
First of all, the nodes that can be used as GC roots are mainly constants, static class properties, and local variable tables in the stack frame.
In the case of accessibility analysis, the condition that the object can be recycled is to refer to it without reference, to judge this requires all GC roots nodes first, if you want to check the reference in a method area that can reach hundreds of megabytes, it will consume a lot of time, the hotspot uses a very direct method, A data structure called OOPMAP directly knows where the object reference is stored.
but not all places are recorded Oopmap, only at the security point will be recorded oopmap information , each method may be divided into several pieces of code according to SafePoint, each piece of code a OOPMAP, scope is naturally limited to this section of code, So each method may have several oopmap. The selection of safe points is usually in places where long execution takes place, such as method invocation, loop jump, exception jump, and so on.
It is worth mentioning that when doing the accessibility analysis, in order to ensure that the consistency must reach the security point in order to pause all Java execution thread start GC, that is, stop the world, So throw a question: how do you get all the threads (not including the JNI calls) running to the nearest security point when the GC occurs and then pause?
There are preemptive interrupt and active interrupt two scenarios, preemptive interrupt is when the GC occurs, the first break all the threads, found that the thread break is not on the security point, the recovery of threads, let it run to the security point, the current virtual machine is not used in this scenario; Active Interrupt is to set a polling flag where the security point and the creation object need to allocate memory, when the GC is disconnected, not directly to the thread, so that each thread executes when it proactively polls the flag , and discovers that the interrupt flag is true when it interrupts its own suspend.
2, Oopmap and remembered set review
Combined with the remembered Set mentioned in the G1 garbage collector, a simple distinction is made between theOopmap record and the reference information of the root node , which makes it fast and accurate to enumerate the root nodes.
The root node is also possible in the old age, there is this situation: in the old age of a GC Root, it refers to a new generation of an object, the new generation of objects can not be cleared, but if we only want to reclaim the new generation of the object is still to find the old age of reference? To allow us to reclaim the new generation without scanning the entire heap, we use remembered set to record the reference relationship between objects in different eras .
3. Safe area
Using the secure point safepoint mechanism will encounter a problem: a thread in the sleep or blocked state, unable to respond to the JVM's interrupt request, go to a safe place to abort the suspension, and the JVM is unlikely to wait for the thread to be allocated CPU time again. In this case, security zone safe region is required to resolve.
A security zone is a code fragment in which the reference relationship does not change, and it is safe to start the GC anywhere in the region.
When a thread executes to a secure zone, it first identifies itself as having entered a security zone, so that during this time the JVM initiates the GC, without having to identify those threads in the security zone, and when the thread leaves the security zone, it checks to see if the system completes the GC and, if the thread is complete, can continue to execute. Otherwise wait to receive a signal that you can leave the safe zone.
Reference Links:
78934379
https://www.jianshu.com/p/d0ab167b460d
Https://www.cnblogs.com/strinkbug/p/6376525.html?utm_source=itdadao&utm_medium=referral
http://dsxwjhf.iteye.com/blog/2201685
JVM Theory: (two/3) garbage collection algorithm, garbage collector