Java garbage collection and memory allocation

Original link: http://www.cnblogs.com/bojuetech/p/6595833.html

Collection algorithms is the methodology garbage collection, the garbage collector is the realization of memory recovery

Automatic memory management solution is: memory allocate memory for objects and reclaim allocated to the object

Why do we have to understand learning and memory allocation GC it?

With the help of JVM automatic memory management mechanism, no longer need to delete / free code for a new pair for each write operation. But when the problem of memory leaks and spills occur, if not understand how to use the virtual machine memory, then troubleshoot the error will be a very difficult task.

GC (garbage collector) in front of pile for recycling, will first determine which objects are "alive", which had been "dead." Then there is the survival of the object decision algorithm.

Object survived decision algorithm

Reference counting algorithm:

ALGORITHM: adding a reference to the object counter, whenever a reference to its place, the counter value by 1, when referring to the failure, the counter value is decremented by 1, the counter at any time for the object 0 is no longer being used.

Advantages: simple, high efficiency is determined

Disadvantages: difficult to solve the problem of mutual circular references between objects

Reachability analysis algorithm:

ALGORITHM: through a series of "GC Roots" object as a starting point, to start the search downward from these nodes, called search path traversed chain of references, when an object is not connected to any reference GC Roots chain, then prove this object is not available.

FIG: object5, object6, object7 although interrelated, but they are not up to the GC Roots, they would be determined to be the object recyclable.

image description

It may include a target GC Roots of:

1. VM stack referenced objects

2. The method of static class attribute references the object region

3. The method of the object in the constant reference

4. A native method stack referenced objects JNI

Life or death?

Even unreachable in reachability analysis algorithm objects, and it is not "Feisibuke", and this time they are temporarily in a "probation" stage, to really proclaim the death of an object, at least twice to go through the labeling process:

If the object is performed after the reachability analysis found the chain of references is not connected GC Roots, then it will be marked the first time and performs a screening with the proviso that if the object is necessary to perform finalize () method, when Object does not cover the finalize () method, or a finalize () method has been invoked over the virtual machine, the virtual machine these two cases are considered "not necessary to perform."

If the object is determined to be necessary to perform a finalize () method, then the object will be placed in a queue called the F-Queue in, and go later by one automatically created by the virtual machine, low-priority thread Finalizer execute it, here called the "execution" is a virtual opportunity to trigger this method, but it does not promise it will wait for the end of the run, the reason for this is: If an object is performed finalize () method is slow, or the occurrence of an infinite loop , will likely lead to F-queue queue of other objects in perpetual wait, Ben even lead to the collapse of the entire memory recovery system.

finalize () method is the last chance to escape death fate of the object, later GC will be F-Queue object in a second small mark, if the object to be successful in their own rescue finalize () method, simply re associate it with any object on the chain of references, such as his own (this keyword) assigned to the member variables of a class or object variable, and that at the second mark will be removed out "will recover" collection, if the object has not escaped this time, it really is that essentially recovered.

In addition, finalize any object () method will only be called once the system automatically. finalize () can do all the work, using a try-finally or otherwise can do better, more timely, it is recommended that you completely forget about the Java language in the presence of this approach. See "In-depth understanding of the Java Virtual Machine"

Garbage collection algorithm

Mark - sweep algorithm:

Algorithm is divided into "mark" and "clear" in two stages:

First of all marked objects need to be recovered, marking the completion of reunification after the recovery of all the objects marked

Shortcomings:

Efficiency, marking and removal efficiency of the two processes is not high.

Space problem: when will generate a lot of discrete memory fragmentation mark after clearing space debris too much may lead to later need to allocate large objects in the program is running, can not find enough contiguous memory and had to trigger another garbage collection in advance action

Replication algorithm:

Algorithm: by available memory capacity is divided into two equal size, uses only one of them. When this piece of memory runs out, the copy will also survive object to another piece of the above, then memory space has been used once and then clean out.

Such that each time the entire semi memory recovery zone, will not consider the complexities of memory fragmentation isochronous memory allocation, as long as the top of the stack pointer movement, in order to allocate memory. Simple, efficient operation. The cost of the algorithm is about memory reduced to half the original, would be too great point.

Mark - Collation Algorithm:

Algorithm: mark all objects need to be recovered, so that all live objects are moved to the end. Then clean out directly to memory beyond the boundary end.

Generational collection algorithm:

Algorithm: Depending on whom the survival period of the memory is divided into a few pieces, usually the old and the new generation of Java heap is divided into years, so you can use the most appropriate collection method based on the characteristics of each era.

In the new generation, each time garbage collection when there are a large number of objects found dead, only a few survive, then copy the selection algorithm. Only need to pay a small amount of live objects replication costs to complete the collection.

The old era because of the high survival rate of the object, there is no extra space is allocated to its guarantee, you must use the "mark - clean-up" or "mark - finishing" algorithm to recover.

New Generation: divided into three spaces, a space Eden and two Survivor spaces.

The vast majority of new objects to be created will be assigned here, since most of the objects becomes unreachable soon after creating so many objects are created in the new generation, then disappear. The object disappeared from this area of ​​the process we call "minor GC".

Old year: object does not become unreachable, and survive from the new generation, it will be copied here. It occupies space than the new generation and more. Also due to its relatively large space, it takes place in the old GC's much less than the new generation. Object disappears from the old era process, which we call "major GC" (or "full GC")

The vast majority has just been created object will be stored in the Eden space. After the execution of the first Eden space GC, live objects are moved to Survivor wherein a space. Then, after the Eden space to perform GC, live objects can be stacked in the same space Survivor. When a spatial saturation Survivor, still live objects are moved to Survivor another space. After that it will clear the already saturated Survivor space.

Repeat several times the object is still alive in the above steps, it will be moved to the old era.

The garbage collector

image description

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.

Before learning all kinds of garbage collector to understand the "Stop the World" under. "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. When the "Stop the World" occurs, in addition to the desired GC threads, all threads are in a wait state until the task is completed GC. GC optimization often refers to reducing the time "Stop the World" occurred.

"Stop the World" this understanding is the image: your mother when you clean the room, certainly make you honestly or in a chair outside the room waiting for, if her while cleaning, while you throw confetti, which rooms also finished cleaning?

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 collectors: Serial old collector's edition, 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 is just about marking objects GC Roots can be directly linked to, 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 threads, but consuming very short.

Concurrent mark is the beginning stage 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 is the mark for amendments made 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.

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

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 idea of ​​a virtual machine using 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 guarantee: Minor GC before it 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.

to sum up

One of the main factors garbage collection and garbage collectors are in many cases affect system performance, concurrency, and 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.

These are my learning "in-depth understanding of the Java Virtual Machine," notes finishing a book.

Reproduced in: https: //www.cnblogs.com/bojuetech/p/6595833.html

Guess you like

Origin blog.csdn.net/weixin_30455067/article/details/94796630