java garbage collection and memory allocation

Objects dead

General judgment whether the object has been "dead" There are two ways, one is 引用计数法, there is a 可达性分析法.

Reference counting algorithm

Adding a reference to an object in the counter, whenever a reference to its place, the counter value is incremented by 1; when referring to the failure, the counter value is decreased by 1; any time counter for the object 0 is impossible to be used.

The java virtual machine not through reference counting algorithm to determine whether the object is alive.

Reachability analysis algorithm

Through a series of objects referred to as "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object has no references to GC Roots chain ( from GC Roots is not up to this object), then it proves that this object is not available.
In the java language, an object can be used as the GC Roots of the following categories:

  • VM stack (local variables table stack frame) referenced objects
  • Method static property class object referenced area.
  • The method of constant reference object region
  • JNI native method stack objects referenced

Talk about references

Sometimes we might have such a demand: When the memory space is also enough, is able to remain in the memory; when memory space after performing garbage collection is still very tight, you can discard these objects.
After jdk 1.2, java i.e. the introduction of reference 4: strong references (Strong Reference), soft references (Soft Reference), weak reference (Weak Reference), the virtual reference (Phantom Reference). Following is a brief introduction of the four reference.

  • Strong references cited generally are created by new, as long as the reference is still strong, the garbage collector never recovered off the object being referenced.
  • Soft references are used to describe some, but not to be there with the object. Before the system is going to happen memory overflow exception, these objects will be recovered. If the recovery has not enough memory, memory overflow exception will be thrown.
  • Weak references are also used to describe non-essential object is associated with a weak reference to an object can only survive until the next garbage collection occurs. When the garbage collector job, regardless of the adequacy of current memory will only recover lost objects are associated with weak references.
  • The only effect is a virtual reference system to receive a notification when the object is recovered collector.

Garbage collection algorithm

Mark - sweep algorithm

The most basic collection algorithm is "mark - sweep" (Mark-Sweep) algorithm, the algorithm is divided into "mark" and "clear" two phases: mark all objects need to be recovered, unified recovery after labeling is complete. Here's mark is marked by reachability analysis algorithm.

This method exists two problems: one is efficiency, marking and removal efficiency of both processes is not high; the other is the space issue, will generate a lot of discrete memory fragmentation mark after clearing space debris too much may lead to future the allocation of large objects, can not find enough contiguous memory and had to trigger another garbage collection operation in advance.

Replication algorithm

In order to address efficiency issues, it available memory is divided into two equal size, uses only one of them. When this piece of memory is used up, it will also copy the live objects to another piece above, then memory space has been used once and then clean out.

Modern commercial virtual machines are using this collection algorithm to recover the Cenozoic, the memory is divided into a larger space of Eden and two Survivor smaller space, and each time Eden where a Survivor. When recycling, the Eden and Survivor also alive objects once copied to another piece of Survivor space, and finally clean out Survivor Eden and just used space.

Mark - Collation Algorithm

According to the characteristics of old age, generally use the "mark - finishing" (Mark-Compact) algorithm, the process is still marked with "mark - sweep" algorithm the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving object moving toward the one end, and then clean out the direct memory outside end border.

Generational collection algorithm

The current garbage collection business virtual machine implements a "zoning collection" (Generational Collection) algorithm, depending on the subject alive the memory cycle is divided into a few pieces. The Java heap is generally divided into the old and the new generation's, using the most appropriate collection method based on the characteristics of each era. That is, in the new generation, each time garbage collection, there will be a large number of dead objects, only a few survived, the choice of replication algorithm. For the old year survival rate is very high in the object, there is no extra space is allocated to its guarantee, you must use the "mark - clean-up" or "tags to organize" algorithm for recycling.

The garbage collector

If the algorithm is a collection methodology memory recovery, then the garbage collector is to recover memory of implementation. This introduces the CMS collector and G1 collector.

CMS collector

CMS (Concurrent Mark Sweep) is a collector for the shortest recovery time objectives pause collector. Its operation process is divided into four steps:

  • The initial mark (CMS initial mark)
  • Concurrent mark (CMS concurrent mark)
  • Relabeled (CMS remark)
  • Concurrent Clear (CMS concurrent sweep)

Among them, the initial marker, re-mark these two steps still need to "Stop The World". The initial mark just mark what objects can be linked directly to the GC Roots, very fast, concurrent mark phase is in progress GC Roots Tracing, and re-mark phase correction is in order during the concurrent mark because the user program continues to run and generate leads mark mark them that part of the object changes, dwell time at this stage usually slightly longer than the initialization phase of some marked, but short of time than the concurrent mark.

G1 collector

G1 (Garbage-First) collection is designed for the garbage collector server applications. G1's features are as follows:

  • Parallel and concurrent
  • Generational collection
  • Spatial integration: with the CMS "mark - clean up" different algorithms, G1 is based on the whole "mark - finishing" collector algorithm is based on the "Copy" algorithm from the local (between two Region) view but in any case, these two algorithms mean no memory space debris during the G1 runs, collected to provide regular memory available.
  • 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 predictable.

The scope of the collection of other collectors before the G1 is a whole new generation or old's, and G1 is no longer the case.
G1 collector operation can be divided into the following steps:

  • Initial labels (Initial Marking)
  • Concurrent mark (Concurrent Marking)
  • The final mark (Final Marking)
  • Filter Recycling (Live Data Counting and Evacuation)

Memory allocation and recovery strategies

java technology system advocated in the final automatic memory management can be attributed to solve two problems for the automation of: allocating memory for objects and reclaim memory allocated to the object.

Objects priority allocation in Eden

In most cases, the new generation of object allocation in Eden area. When the Eden area is not enough space allocated, the virtual machine will launch a Minor GC.

Large objects directly into the old year

The so-called large object refers to a Java object requires a lot of contiguous memory space, the most typical of large objects is the kind of long strings and arrays.

Long-term survival of the object will enter the old year

The virtual machine to the idea of ​​generational collection to manage memory, it must be able to identify which objects should be in the new generation of memory when it recovered, which objects should be placed in the old era. To do this, a virtual machine for each object defines a target age (Age) counter. If the object was born in Eden, and after the first Minor GC still alive, and can accommodate Survivor, it will be moved to Survivor space, and age of the subject is set to 1. Objects Each time through the Minor GC, increased age, 1 year old, the age when it is to a certain extent (the default is 15 years old), it will be promoted to the old era.

Age Determination of dynamic objects

In order to better adapt to different conditions program memory, the virtual machine is not always subject to the requirements of the age must be promoted in order to reach Max TenuringThreshold years old, if the sum of the same age Survivor space is larger than the size of all the objects in the space of half Survivor, age objects greater than or equal to the age you can go directly years old, no need to wait until the age MaxTenuringThreshold in requirements.

to sum up

The above stated mainly made reference to the "in-depth understanding of the Java Virtual Machine," the third chapter 垃圾收集器与内存分配策略, mainly on the algorithm to determine whether java objects in death, garbage collection algorithm, and introduces two kinds of CMS and G1 garbage collector.

references

  • "In-depth understanding of the Java Virtual Machine"

Guess you like

Origin juejin.im/post/5d2fdfa96fb9a07f070e62aa