JVM- garbage collection

definition

Garbage collection is a mechanism to find out which objects are used, what the object is not used, and the mechanism will be deleted in the latter heap memory.

Using so-called objects (referenced object), refers to the program contains a pointer to the object; unused objects (objects not referenced), the pointer is not used by any given point, thus occupying memory may be recovered out.

Garbage collection

eden->s0("from")->s1("to")->tentired

eden region shown above, s0 ( "From") region, s1 ( "To") belong to the new generation area, tentired area belongs year old. In most cases, the object will be assigned in the Eden area, after a new generation garbage collection, if the object is still alive, will enter the s1 ( "To"), and the age of the subject will add 1 (Eden District -> Survivor District after the initial age of the object becomes 1), when its age to a certain extent (the default is 15 years old), will be promoted to the old era. Objects promoted to the age threshold of old age, by the parameters -XX: to set MaxTenuringThreshold. After this GC, Eden area and "From" area has been emptied. This time, "From" and "To" will exchange their roles, which is the new "To" is before the last GC "From", the new "From" that is, before the last GC "To". In any case, we will guarantee an area called To Survivor is empty. Minor GC will repeat this process until the "To" area is filled, after the "To" area is filled, all the objects will move to the old generation.

Automatic memory management in Java

Java's automatic memory management is allocating memory for objects and reclaim the object's memory.

Meanwhile, the core functionality of the Java memory management is automatic heap allocation and recovery of objects in memory .

Common heap memory allocation strategy

  • Objects priority in the distribution area eden
  • Large objects directly into the old year
  • Long-term survival of the object will enter the old year

Objects priority in the distribution area eden

The current mainstream garbage collector will adopt a generational collection algorithm, it is necessary to heap memory is divided into the old and the new generation's, so that we can choose the appropriate garbage collection algorithm based on the characteristics of each era.

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

Large objects directly into the old year

Large objects that require a lot of memory space of continuous objects (for example: strings, arrays).

In order to avoid copy to allocate memory due to the large object allocation guarantee mechanism to bring decreases efficiency.

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

Since 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 in the old era. To do this, the virtual machine to each object a target age (Age) counter .

If the object was born in Eden and after the first Minor GC after still survive, Survivor and can be accommodated, it will be moved to Survivor space, and the age is set to 1, the object survived every once in MinorGC in Survivor, 1 year of age increases, when its age to a certain extent (the default is 15 years old), will be promoted to the old era.

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.

MinorGC and FullGC difference

  • 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 .

How to determine the target dead

  • Reference counting
  • Reachability analysis algorithm

Almost all of the heap stood object instance, for the first step is to determine the recycling of garbage in front of those objects have died (that is, objects can no longer be used in any way).

Reference counting

Add a reference to an object counter, whenever a reference to its place, the counter is incremented; when referring to failure, the counter is decremented by 1; any time the counter for the object 0 is no longer being used.

This method is simple, high efficiency, but the current mainstream virtual machine did not choose this algorithm to manage memory, which is the main reason it is difficult to solve the problem of mutual circular references between objects

Reachability analysis algorithm

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.

GC Roots objects

In JAVA GC Roots can be used as the main targets are:

  • 1, the virtual machine stack referenced object.
  • 2, a method class object reference region static properties.
  • 3. The method of constant reference object region.
  • 4, native method stacks JNI object references.

Unreachable object is not "Feisibuke"

Even unreachable in reachability analysis of objects, and it is not "Feisibuke", and this time they are temporarily in a "probation period" to really proclaim the death of an object, at least twice to go through the labeling process ; up analysis of law unreachable object is marked the first time and conduct a screening filter 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.

It is determined to be the object needs to be performed will be placed in a queue for the second mark, unless the object is associated with any reference to an object on the chain, otherwise it will be really recovered.

Talk about references

Whether it is by reference to the number of reference counting method to determine the object, or whether by reachability analysis to determine the object's reference chain up to determine the object's survival with the "reference" relevant.

Before JDK1.2, Java referenced definition is very traditional: If the reference value represents the type of data storage is another piece of memory starting address, he said to represent a piece of memory references.

After JDK1.2, Java concept was expanded references, the references are divided into strong references, soft references, weak references, phantom references four kinds (reference intensity gradually weakened)

Strong references

As long as there is a reference exists, will never be recovered out

Before we actually use most of the references are strong references, which is the most commonly used reference. If an object has a strong reference, it is similar to essential household items , the garbage collector will never recover it. When the memory space is insufficient, Java Virtual Machine would rather throw OutOfMemoryError errors, abnormal termination of the program, it will not recover by random objects that have strong references to solve the problem of insufficient memory

Soft references (SoftReference)

Generally it refers also used, but not necessarily object. In low memory situations, this section will recover lost memory, if enough memory overflow exception will be thrown.

If an object has only soft references, it is similar to the non-essential household items . If the memory space is sufficient, the garbage collector does not reclaim it, if memory space is insufficient, it will reclaim the memory of these objects. As long as the garbage collector does not reclaim it, the object can be used by the program. Soft references are used to implement memory-sensitive caches.

Soft references can be a reference queue (ReferenceQueue) used in combination, if the soft reference object referenced is garbage, JAVA virtual machine will be added to this soft references cited associated queue.

Weak references (WeakReference)

Generally refers to non-essential object references even weaker than soft, it can only survive before the next garbage collection, if the garbage collection occurs once, it will be recycled out.

If an object has only a weak reference, it is similar to the non-essential household items . Weak and soft references cited difference is that: only a weak reference objects have more short life cycle. In the process of the garbage collector thread scans memory area under its jurisdiction, once the objects found only a weak reference, regardless of the current memory space is sufficient or not, will reclaim its memory . However, the garbage collector is a low priority thread, and therefore will not necessarily quickly find objects that have only a weak reference.

Weak references can queue and a reference (the ReferenceQueue) used in combination, if the object referenced by the weak reference is garbage, Java virtual machine will be added to this reference weak reference associated queue.

Virtual reference (the PhantomReference)

A reference to the weakest relationship, unable to get a virtual instance of an object by reference. System is able to receive a notification when the object is recovered for the sole purpose of setting a virtual reference objects.

"Virtual Reference" by definition, is non-existent, and several other references are different, virtual reference and does not determine the object's life cycle. If an object holds only a phantom reference, and then it does not have any references, they are likely to be garbage at any time.

Phantom reference objects are mainly used to track the activities of recycled garbage .

And reference must reference the virtual queues (the ReferenceQueue) in combination

A difference between the virtual reference and references cited soft and weak

And reference must reference the virtual queues (the ReferenceQueue) used in combination . When the garbage collector is ready recovery of an object, it is found that if there is a virtual reference, will be recovered before the memory, this reference is added to the virtual queue associated with a reference. Determining whether the program can reference it has been added to the virtual queue references to see if the referenced object is to be garbage collected . If the program finds a phantom reference has been added to the reference queue, then we can take the necessary action before it is recycled in memory referenced object.

Special attention, rarely use weak references cited and in many cases is true, use soft references in programming, because the soft references can accelerate the JVM garbage recycling rate of memory, you can maintain the safe operation of the system to prevent memory overflow produce (OutOfMemory) and other issues .

The more common situation of an object is determined recyclable objects

1. Display the assignment will be null or a reference to the already pointing to an object reference to the new object

For example the following code:

Object obj = new Object();
obj = null;
Object obj1 = new Object();
Object obj2 = new Object();
obj1 = obj2;

2. The local reference object points

For example, the following code:

void fun() {
 
.....
    for(int i=0;i<10;i++) {
        Object obj = new Object();
        System.out.println(obj.getClass());
    }   
}

After each execution cycle time, the generated Object object will become the object recoverable.

3. Only weak references associated with the object

WeakReference<String> wr = new WeakReference<String>(new String("world"));

Permanent generation garbage collection

Note that, in the region outside the reactor there is a permanent substituting substituting (Permanet Generation), which is used to store Class class, constants, methods, etc. described. Recovery Recovery of the permanent generation of two parts: discarding useless classes and constants .

How to determine discarded constant is a constant

Runtime constant pool is primarily recovered waste constant. So how do we judge a constant is the constant abandoned it?

If the presence of the string "abc" in the constant pool, if no current String object reference string constant, then it shows the constant "abc" is discarded constant if the memory, then the recovery time of the occurrence and if necessary, "abc" it will be cleared constant pool system.

Note: JDK1.7 and later versions of the JVM runtime constant pool has shifted from the method area, opened up a storage area for running in the Java heap (Heap) time constant pool

How to determine the class of a class is useless

The main area of ​​recovery method is useless class, how to tell if a class is useless like it?

Constant is determined whether or not a "waste constant" is relatively simple, and to determined whether a class is "useless class" is relatively many harsh conditions. Class needs to meet the following three conditions in order to be regarded as "useless class":

  • All instances of the class have been recovered, which is the Java heap any instance of the class does not exist.
  • Load class ClassLoader has been recovered.
  • Java.lang.Class corresponding to the class object is not referenced in any place, not by the method of accessing the class reflected anywhere.

Virtual machines can be kind of useless meet the above three conditions are recovered, said here is just "may", rather than as objects and not used will inevitably be recycled.

Some concepts

Stop the world concept

JVM GC due to perform to stop the execution of the application, and this may occur in any of the GC algorithm. When the Stop-the-world occurrence, in addition to the desired GC threads, all threads are in a wait state until the task is completed GC. In fact, GC optimization often refers to reducing the time Stop-the-world occurrence, so that the system has the characteristics of high throughput, low pause.

Because when garbage collection, you need to reference the entire state remains unchanged, otherwise it is determined that the determination of garbage, so I recovered later when it is referenced, which all hell broke loose. So, GC, when all other program execution is paused, stuck.
Fortunately, this Caton is very short (especially the new generation), minimal impact on the program (such as with respect to other concurrent GC GC and the like, are not discussed here).
So Caton GC of the resulting problems, is forgiven, temporarily inevitable.

Parallel and concurrent concept

  • 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.

Throughput

Throughput is the total elapsed time and the CPU CPU time used to run user code ratio, i.e., a certain time = running user code / (user code running time + time garbage collection).
A virtual machine running 100 minutes, in which garbage collection spend one minute that the throughput is 99%.

Guess you like

Origin www.cnblogs.com/xiaohaigui/p/10986062.html