Garbage collection and memory allocation

Foreword

Recovering the memory garbage collection algorithm methodology; garbage collector is embodied memory recovery;

Automatic memory management is: to allocate an object and recycling of waste of memory occupied by the object.

Garbage collection ranges: the Java heap and method area.

First, the object is sentenced to live algorithm

1) reference counting algorithm (mainstream virtual machine has not adopted): to add a reference to an object counter, whenever there is a place to reference when it counter is incremented; when referring to fail, the counter value is decreased by 1; any time when a object reference counter is 0, it becomes to be discarded. It can be recycled.

Advantages: simple, high efficiency is determined;

Disadvantages: difficult to solve mutual problems cycles between object references.

2) reachability analysis: ALGORITHM is referred to as a series of "GC Roots" object as a starting point, the search downwards from these nodes, called search path traversed chain of references, when an object to " GC Roots "without any reference to the chain, then the object is not available.

As "GC Roots" object comprising:

1, the virtual machine stack referenced objects;

2, the method area static property class object reference;

3. The method of constant reference object region;

4, native method stacks in the JNI (i.e. native General Method) object reference.

3) determine the life or death

Even unreachable in reachability analysis method of the object, nor is it to be immediately recovered. Because to really be considered to be discarded for recycling, at least to go through the "mark" the two events.

When an object is not referenced in the chain between the series is first found and "Gc Roots" node, then it will be marked once and for primary screening, if screening is necessary to perform that finalize () method. When the object is not covered finalize () method, or a finalize () method has been the virtual machine called. The virtual machine as "there is no need to perform."

If the object is determined to be necessary to perform a finalize () method, then the object will be prevented in a F-Queue queue and executed later by the a low priority thread. finalize () method is the last chance to escape the object to be recycled garbage. Later F-Queue will mark a second time. If the period has not been established relationships with any object, and that after the second mark to be basically really recovered.

Second, the garbage collection algorithm

1) mark - sweep algorithm

Clear thinking mark algorithm consists of two phases, the first phase, all unreachable mark of "junk" reachability analysis algorithm based on the second stage, the direct release of the memory space occupied by these objects.

Advantages: is the basis of ideological garbage collection algorithm, the subsequent recovery algorithms are improved based on this algorithm.

Disadvantages:

  • Efficiency: mark and sweep efficiency is very low, we need to traverse twice heap.
  • Space problem: the clear mark, will produce large amounts of memory fragmentation. Once the need to allocate large objects, often to once again trigger a garbage collection.

2) replication algorithm

The memory is divided into two equal-size blocks, each of which only one, when the system initiates GC collection action, all copied to another block of the current block is still surviving object and release the current monolithic block total memory space.

Modern commercial virtual machine: are using this collection algorithm to recover the target area of ​​the new generation. The memory is generally divided into a larger piece of Eden and two Survivor smaller space. The default ratio is 8: 1: 1.

In this case, memory usage will increase from 50-90%, but even so, we can not guarantee that every recovery are only more than 10% of the objects survived. This time, it is necessary to perform memory allocations guaranteed by other memory (primarily years old).

3) mark Collation Algorithm

Since the object replication algorithm at a higher survival rate, it is necessary to perform more copy operations, efficiency will become very low, but the key is you do not want to waste 50% of memory, you need to have additional space for memory allocation guarantee to deal with extreme cases of more than 90 & live objects. So in the old era is generally not directly using this algorithm.

According to the characteristics of old age, proposed mark - sort algorithm, in fact, enhanced version of the mark - sweep algorithm. But is not directly recyclable objects directly cleared. It is to move the live objects in the same side, and then recovering memory outside the boundary.

4) generational collection algorithm

The current mainstream jvm virtual machine, and are based on "generational collection" algorithm, which is not a new idea, but according to the characteristics of the object survival period, the java heap into a few pieces. The new generation and the old era. "New Generation" is generally a short time a large number of abandoned objects using high replication efficiency of the algorithm. "Old Age" the general object of high survival rates with no additional memory space for memory allocation guarantee, you must use the "mark - clean-up" or "mark - sweep" of the algorithm.

Third, the garbage collector

FIG acting on the different generational garbage collector, if there is the connection between the two collectors, it can be used with. Area where the virtual machine, then it belongs to the new generation of collector's collector or old

"Stop the World" will take place in any of the GC algorithm. "Stop the World" means that the JVM due to the implementation of GC and stopped the execution of the application.

  • Serial Collector: single-threaded, the new generation of collectors, using the copy algorithm. It will only use a CPU or a collection thread to complete garbage collection, garbage collection is performed, must "Stop the World", pause for all his work thread until it collects end.

  • ParNew Collector: multithreaded version Serial collector, control parameters collection algorithms, Stop the World, object allocation rule, recovery strategies are exactly the same with the Serial Collector

  • Parallel Scavenge collector: Mesozoic collector, using the copy algorithm, parallel multi-threading.

  • Serial Old Collector: old's version of the Serial collector, single-threaded, using the mark - Collation Algorithm.

  • Parallel Old collectors: Parallel Scavenge collector's version of the old, multi-threaded, using the mark - Collation Algorithm

  • CMS collector: a kind of pause obtain the shortest recovery time objectives collector, based on the "mark - sweep" algorithm. Operation process in four steps: initial mark, concurrent mark and re-mark, concurrent cleared.

    The initial mark, 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 then re-mark phase because the user program is to continue to operate during the correction caused by concurrent mark mark mark produced record that part of the object changes, dwell time at this stage than the initial general mark phase a little longer, but far more concurrent mark time period.

    Throughout the process the longest concurrent mark and concurrent removal process, the collector threads can work with user threads, so the memory recovery process CMS collector is with the user concurrently executing threads.

    Advantages: concurrent collection, low pause

    Disadvantages: very sensitive to CPU resources to deal with floating garbage, sweep algorithm based on the mark at the end of a large number of controls collected debris

  • G1 Collector: G1 collector collector technology is one of the most cutting-edge development outcomes oriented garbage collector server applications.

    G1 features: parallel and concurrent, generational phone, spatial integration, predictable pause

    The operation process is as follows: initial mark, concurrent mark, the final mark, filter recycling.

    The initial mark phase: just mark what objects can be linked directly to the GC Roots, and modify the value of TAMS, so that the next phase of the user program to run concurrently, can create new objects in the correct available Region, this phase needs to pause a thread, but it takes very short.

    Concurrent mark phase: the beginning of the heap objects reachability analysis from GC Roots, find live objects, this stage takes a long time, but may be complicated by the user program execution.

    The final stage marks: it is to be amended during the concurrent mark due to the continued operation of the user program which led to marked changes in that part of the generated record mark, the virtual machine during this time the object changes recorded in the thread Remembered Set Logs inside. The final stage marks the need to merge Remembered Set Logs data into Remembered Set, which need to pause the thread stage, but can be executed in parallel.

    Finally, the recovery in the screening stage: First, the recovery value and cost of each Region of the sort. According to users expect GC pause time to develop recycling programs.

Fourth, memory allocation and recovery strategies

Objects priority in allocation of 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: large objects are Java objects requires a large amount of contiguous memory control, 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, memory recall when it must be able to identify which objects should be placed on the new generation, which objects should be placed on the old era. To do this, a virtual machine for each object defines a target age counter. If the object was born in Eden and after the first Minor GC after still alive, and can accommodate Survivor, it will be moved to Survivor space and age is set to 1, each object "survive" in the area once Minor Survivor 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: the virtual machine is not always on the age requirements must be met before promotion of MaxTenuringThreshold years old, if the sum of the same age Survivor space is larger than half the size of all the objects in space Survivor, age greater than or equal to the target age on direct access to the old era, no need to wait until the age MaxTenuringThreshold in requirements.

Space allocation guarantees: Before Minor GC occurs, first check whether the old virtual machine's maximum available contiguous space is greater than all the objects in the new generation of total space, if this condition is met, then the Minor GC to ensure that it is safe. If not satisfied, the virtual opportunity to review whether to allow HandlePromotionFailure settings guarantee failure.

V. Summary

One of the main factors garbage collection and garbage collectors are in many cases affect system performance, concurrent capacity , the reason why the virtual machine offers a variety of collectors and provide a large number of adjustable parameters, because only according to actual needs and achieve choose the best way of collection methods in order to obtain maximum performance. There is no fixed collector, parameter combination, there is no optimal tuning method, a virtual machine, there is nothing inevitable garbage collection behavior.

Guess you like

Origin juejin.im/post/5d3085eaf265da1ba2529857