Garbage collector Overview

Source: Internet
Author: User

Issues to consider for garbage collection
1. Which memory needs to be recycled? 2. When to recycle? 3, how to recycle?

How can I tell if an object is "dead"? First, Reference counting----Reference counting method

1. Algorithm idea

Add a reference counter to the object, and whenever there is a reference to it, the value of the counter is incremented by one, and when the reference fails, the counter is reduced by one. At any moment, an object with a counter value of 0 is no longer possible to be used.

2. Cases with this approach

Microsoft's COM technology, using ActionScript's flash Player,python language and squirrel scripts

3. Features

The implementation is simple and efficient, but the mainstream Java virtual machine is not in this way because it does not solve the problem of circular references.

public class REFERENCECOUNTINGGC {public Object instance = null;private static final int _1MB = 1024 * 1024;//The function of this property is to occupy points Memory to see if the memory is recycled in the GC log private byte[] bigsize = new byte[2 * _1MB];p ublic void Testgc () {REFERENCECOUNTINGGC Referencecounti ngGC1 = new REFERENCECOUNTINGGC (); REFERENCECOUNTINGGC referenceCountingGC2 = new REFERENCECOUNTINGGC ();//circular reference referencecountinggc1.instance = referencecountinggc2;referencecountinggc2.instance = REFERENCECOUNTINGGC1;REFERENCECOUNTINGGC1 = null; ReferenceCountingGC2 = null; System.GC ();} public static void Main (string[] args) {new REFERENCECOUNTINGGC (). TESTGC ();}}


Virtual machines do not recycle them because they are not referenced by the two objects, indicating that the virtual machine does not use reference counting to determine whether an object is alive or not.

Two, reachability analysis algorithm of accessibility

In the mainstream business programming language, Java,c#,lisp is used by the accessibility analysis algorithm to determine whether an object is alive or not.

1. Algorithm idea

Searching through a series of called GC Roots objects as starting points, starting from these nodes, is called the reference chain. This object is unreachable when an object is not connected to the GC roots (that is, the GC of graph theory is roots to this object).

2. Cases with this approach

Java,c#,lisp and other languages

3. The object that can be used as GC roots in Java
  • Objects referenced in the virtual machine stack (local variable table in the stack frame)
  • Object referenced by class static property in method area
  • Objects referenced by constants in the method area
  • Objects in the local method stack that are referenced by JNI
Iii. references

Whether it is a reference counting method or a accessibility analysis algorithm, the reference is used to determine whether an object survives.

After JDK 1.2, the concept of reference was extended by Java, which is described in detail below.

1. Strong citation (strong Reference)

References that are common in program code, such as Object object = new Object (), are strongly referenced. As long as a strong reference exists, the garbage collector never reclaims the referenced object.

2. Soft reference (Soft Reference)

Describes some objects that are useful but not necessary. For objects that are associated with soft references, these objects are listed for a second collection before the system will have a memory overflow, and an Oom exception is thrown if the collection does not have enough memory. Java provides a SoftReference class to implement soft references.

3. Weak references (Weak Reference)

It is also used to describe non-essential objects, but the strength is weaker than soft references, and the objects associated with the reference can only survive until the next garbage collection. When the garbage collector is working, the objects that are only referenced are recycled, regardless of the current memory adequacy. Java provides a WeakReference class to implement weak references.

4. Virtual reference (Phantom Reference)

Also known as Phantom references or phantom references. Is the weakest kind of reference relationship. The existence of a weak reference to an object does not affect its lifetime at all, nor can it obtain an instance of an object through a virtual reference. The only purpose of setting a virtual reference for an object is to be able to receive a system notification before the object is reclaimed by the garbage collector. Java provides a phantomreference class to implement a virtual reference.

Four, survival or death

Cannot reach the object is to be sure to die, be recycled?

No. To truly declare death, an object must undergo at least two marking processes.

First mark: If the object finds no reference chain connected to the GC roots after the accessibility analysis algorithm, it will be first marked and filtered, and the condition is whether this object is necessary to execute the Finalize () method. When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine considers that both cases are considered necessary for execution to be judged dead.

If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue called F-queue and then executed by a low-priority finalizer thread that is automatically created by the virtual machine at a later time. The execution here only refers to the virtual opportunity to trigger this operation, but does not promise to wait for his run to finish. The reason for this is that if an object has a dead loop in the Finalize () method, slow execution is likely to cause other objects in f-queue to wait permanently, or even crash the entire memory-recycling system.

Second mark: The Finalize () method is the last chance for an object to escape the fate of death, and later the GC will make a second small-scale mark on the object in F-queue if the object is to be in Finalize () Save yourself----just to re-establish a relationship with any object on the reference chain, such as assigning yourself (the This keyword) to a member variable of an object, it will be removed from the collection of "going to be recycled" at the second mark. If you have not escaped at this point, then basically it is really recycled.

Note: The Finalize () method of any object is automatically called only once by the system.

Garbage collector Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.