In-depth understanding of JVM: Garbage Collection

First, the learning garbage collection necessity

When you need to troubleshoot a variety of memory overflow problems when the garbage collection becomes a bottleneck when the system reaches a higher concurrency, we need to implement the necessary monitoring and regulation of these "automation" technology.

Two, JVM memory allocation and recovery

Garbage collection is mainly heap memory of the virtual machine. Stack diagram is as follows:

eden region shown above, s0 ( "From") region, s1 ( "To") belong to the new generation area, tentired area belongs year old.

Two kinds of garbage collection

  • New Generation GC (Minor GC) : refers to the new generation of garbage collection action takes place in, Minor GC very frequently, recovery speed is generally faster.
  • Old's GC (Major GC / Full GC) : refers to the place in GC old age, appeared in Major GC often accompanied by at least one of the Minor GC (not absolute), Major GC's speed is generally slower than Minor GC of more than 10 times .

Allocate memory for the object

Eden region shown above, s0 ( "From") region, s1 ( "To") belong to the new generation area, tentired area belongs year old

In most cases, the first target will be the memory allocation in Eden above. If there is not enough space for time. Will first perform a Minor GC, the GC typically occurs in eden and s0 ( "from"), if there are live objects to enter s1, and age of the subject will add 1 (Eden District -> Initial ages after Survivor areas subject becomes 1), when its age to a certain extent (the default is 15 years old), will be promoted to the old era. After this GC, eden and s0 area have been emptied, then swap s1 and s0, live objects will also experience the next Minor GC, if still alive, so the age will be + 1, Minor GC will repeat this process, until the "to" area is filled, after the "to" area is filled, all the objects will be moved to the old era.

Common heap memory allocation strategy

(1) assign a priority target in the Eden area (2) large objects directly into the old year (3) long-term survival of the object will enter the old year

Age determination of dynamic objects

In order to better adapt to different circumstances the program memory, the virtual machine is not always required target age must reach a certain value in order to enter the old era, if the sum Survivor space of the same age is more than half the size of all the objects in space Survivor, age greater than or equal to objects that age you can go directly years old, need not reach the age requirements.

Third, the object is already dead?

Reachability analysis

The basic idea is that through a series of algorithms called  "GC Roots"  objects as a starting point, to start the search down from these nodes, the nodes traversed path is called a chain of references, when an object has no references to GC Roots chain connected, then it proves that this object is not available.

72762049.jpguploading.gifUploading ... re-upload canceled

GC Roots can be used as an object:

(1) Virtual Machine stack (Local Variable Table stack frame) referenced object.

Class (2) method of the object referenced by the static properties zone.

Object references (3) Method constant region.

(4) In the JNI native method stacks (i.e. local method) of the object reference.

Whether the object of death

(1) marked the first time: reachability analysis unreachable objects are screened. Screening condition is whether this object is necessary to perform the finalize method. When the object is no finalize method, or finalize method has been covered call obsolete virtual machines, virtual machines, these two cases deemed unnecessary to perform.

(2) Second marking: objects to be screened is placed in a queue F-Queue, GC heap after a second F-Queue small tag. The queue will finalize the implementation of the method.

Objects can also save themselves, in the finalize method is with reference to their associated objects can be any of a chain, you can be removed from the queue.

But only in order to save themselves, because any system objects finalize method will only be called once.

Fourth, references

(1) A strong reference: Obeject obj = new Object (); such references garbage collector never recovered off the object being referenced.

(2) Soft Quote: There are, but not necessarily with the object, the system will occur before the memory overflow exception, these objects will be included in the scope of recovery for the second recovered.

(3) a weak reference: to survive only until the next garbage collection occurs. When the garbage collector job, regardless of whether enough memory, will be recovered

(4) an imaginary reference: weakest. Phantom reference objects are mainly used to track the recovery of waste activities.

Fifth, the garbage collection algorithm

(1) mark - sweep algorithm

The algorithm is divided into "mark" and "clear" stage: first mark all objects need to be recovered, all unified object is marked recovery after labeling is complete. It is the most basic collection algorithm, subsequent algorithms are improved to give its deficiencies. This garbage collection algorithms will bring two obvious problems: inefficiency and memory fragmentation.

å¬ä¼å·

(2) replication algorithm

In order to address efficiency issues, "copy" collection algorithm appeared. It can be the same size memory into two, each one of which is used. When the memory usage after this one, it will also live objects copied to another piece to the space and then used once and then clean out. This allows each recovered memory is the memory of half the interval for recycling.

å¬ä¼å·

This algorithm currently used generational algorithm used in the new generation, the new generation is not in accordance with the 1: 1 is divided, but is divided into (eden + s0) s1 and the two parts.

(3) mark - Collation Algorithm

According to a feature of the proposed labeling algorithm years old, still marking process and "mark - sweep" algorithm is the same, but not directly next steps for the recovery of recyclable objects, but to all surviving objects move toward one end, and then directly to clean out memory other than the end border.

æ ° E® -æ'çç®æ³

The algorithm used to use years old,

(4) generational collection algorithm

The current garbage collection virtual machine implements a generational collection algorithm, which is nothing new idea, but according to different objects survive periods of memory into a few pieces. Generally the java heap into the new generation and the old era, so that we can choose the appropriate garbage collection algorithm based on the characteristics of each era.

For example, in the new generation, each collection will have a large number of objects to die, so you can choose to copy algorithm, only need to pay the cost of reproduction of the small number of objects to complete each garbage collection. The old target's chance of survival is relatively high, and there is no extra space is allocated to its guarantee, so we must select the "mark - sweep" or "mark - finishing" algorithm for garbage collection.

Sixth, the garbage collector

(1) Serial collector

This is a single-threaded collector, when it's garbage collection, threads must suspend all other work.

46873026.jpguploading.gifUploading ... re-upload canceled

Advantages: simple and efficient.

Cons: give users a bad experience, there is a sense of pause.

(2) ParNew collector

This is a multi-threaded version of a serial,

ParNew æ¶éå¨

It is the first choice of many virtual machines running in Server mode, in addition to Serial collector, it only works with CMS collector (concurrent collector in the true sense, will be introduced later to).

The concept of parallel and concurrent added:

  • Parallel (Parallel)  : refers to the number of threads in parallel garbage collection, but this time the user thread is still in a wait state.

  • Concurrency (Concurrent) : refers to the user thread and garbage collection thread while performing (but not necessarily parallel, may be performed alternately), the user program continues to run, and the garbage collector runs on another CPU.

(3) Parallel Scavenge collector

Parallel Scavenge collector also use the copy algorithm multithreading collector,

Parallel Scavenge collector concern is throughput (efficient use of CPU). Focus CMS and other garbage collector pause time is more user threads (improving user experience). Throughput is called the CPU CPU time for the ratio of the total user code running time consuming.

(1) (2) (3) using the new generation of replication algorithm, using old's mark - Collation Algorithm.

(4) Serial Old collectors

Old's version of the Serial collector , it is also a single-threaded collector. It has two main purposes: One use is in JDK1.5 and previous versions and Parallel Scavenge collector with the use of another use is as a fallback plan, CMS collector.

(5) Parallel Old collectors

Parallel Scavenge collector's version of the old . The use of multi-threading and "mark - finishing" algorithm. Focus on throughput and CPU resources of the occasion, it can give priority to Parallel Scavenge collector and Parallel Old collector.

(6) CMS collector

CMS (Concurrent Mark Sweep) is a collector for the shortest recovery time objectives pause collector. It is consistent with the focus on user experience in using the application.

CMS (Concurrent Mark Sweep) HotSpot virtual machine collector is the first true concurrent collector in the sense that for the first time realized the garbage collection thread and let the user thread (basically) the same time.

Using mark - sweep "algorithm

Initial labels:  suspend all other threads, and the recording object directly connected to the root, fast;

Concurrent mark:  simultaneously turned GC and user thread, with a closure structure to record up to the object. But at the end of this stage, this does not guarantee a closure structure that contains all the current up to object. Because the user thread may be continuously updated reference field, so the GC thread can not guarantee the accessibility of real-time analysis. So this algorithm in place will keep track of these updates happen references.

Relabeled:  re-marked during the correction phase is to concurrent mark as a result of the user program continues to run that part of the object of the record mark mark fluctuates, the dwell time at this stage usually slightly longer than the time the initial phase of the mark, far more than the concurrent mark phase short time

Concurrent clear:  open the user thread, while the GC thread starts to do the cleaning for the marked area

CMS åå¾æ¶éå¨

 

(7) G1 collector

G1 (Garbage-First) is a server for a garbage collector, mainly for the same time with multiple processors and large memory capacity of the machine. High probability to meet the requirements of GC pause times, further comprising a high throughput performance characteristics.

An important feature of evolution is considered JDK1.7 the HotSpot virtual machine. It has the following characteristics:

  • Parallel and Concurrent : G1 can make full use of the advantages of hardware under the CPU, multi-core environment, the use of multiple CPU (CPU or CPU core) to shorten the Stop-The-World pause time. Some of the other collectors would otherwise require GC pause Java thread execution action, G1 collector still allows java program by concurrent way to proceed.
  • Generational collection : Although the G1 may not be required with other collectors will be able to independently manage the entire GC heap, but still retains the concept of generational.
  • Spatial integration : with the CMS "mark - clean up" different algorithms, G1 is based on the "tags to organize" collection algorithm implemented on the whole; is based on the "Copy" algorithm from the local point of view.
  • Predictable pause : This is the G1 relative to the other big advantage of CMS, G1 is reduced dwell time and the CMS common concern, but in addition to the pursuit of low-G1 pause, the pause time model can establish a predictable, allowing use by explicitly specifying a time segments of length M in milliseconds.

Operation G1 collector roughly divided into the following steps:

  • The initial mark
  • Concurrent mark
  • The final mark
  • Filter Recycling

G1 collector in the background maintains a priority list, according to each collection time allowed, the maximum value of the preference Region recovery (which is the origin of its name is Garbage-First) . This memory space is divided using Region and prioritized region recovery methods, to ensure that the collector GF limited time collection efficiency can be as high as possible (the memory dismembered).

Reference: https://snailclimb.gitee.io/javaguide/#/docs/java/jvm/JVM%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6?id= _4-% E5% 9E% 83% E5% 9C% BE% E6% 94% B6% E9% 9B% 86% E5% 99% A8

 

Published 134 original articles · won praise 91 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_44588495/article/details/104097669