Java garbage Collection mechanism notes

Source: Internet
Author: User
Tags compact

Java garbage Collection mechanism note the meaning of Java garbage collection
    1. Make sure that the memory space of the object that is no longer referenced is recycled.
    2. Ensure that the referenced object's memory is not reclaimed by error.
    3. Allocate memory again.
Common methods for Java garbage collection reference count collectors

Each object in the heap (not a reference to the object) has a reference count. When an object is created, assign a variable to the object, and the variable count setting is set to 1. When any other variable is assigned a reference to this object, the Count plus 1 (a=b, the object count of B refers to + 1), but when a reference to an object exceeds the life cycle or is set to a new value, The count of references minus 1 (a=c, a no longer points to the object that B points to, and the object to which the C reference points, so B refers to the object count-1, and the +1 of C). Any object with a reference count of 0 can be garbage collected. When an object is garbage collected, it references any object count minus 1 (a=null, at which point the C refers to the object Count-1).

Advantage: The reference count collector can be executed quickly. Disadvantage: cannot detect a circular reference. For example

class Main{    public static void main(String[] args){        Person zhang = new Person();        Person wang = new Person();        zhang.object = wang;        wang.object = zhang;        zhang = null;        wang = null;    }}class Person{    public Object object = null;}

Although the two objects of Zhang and Wang have been assigned to Null,zhang and Wang's objects are not accessible, the reference count for both Zhang and Wang is not 0 because they are applied to each other.

Object reference Traversal (trace collector)

Object references start with a set of objects and recursively determine the reachable (reachable) object along each link on the entire object graph. If an object cannot arrive from one (at least one) of these root objects, it is garbage collected. There will be three of processes,

    1. Tag (marking) object that marks which objects can be reached.
    2. Clears (sweeping) objects that are not attainable.
    3. Compresses (compacting) the remaining objects, re-organizes the objects in memory to form the memory space that can be exploited.
Typical garbage collection algorithm mark-sweep (marker-purge) algorithm

Divided into two stages, the first stage is to mark all objects that are recycled, and the second stage is to reclaim the space occupied by the tagged object.

The disadvantage of this algorithm is that it is easy to generate memory fragmentation.

Copying (copy) algorithm

In order to solve the disadvantage of the mark-sweep algorithm, the replication algorithm divides the memory into two blocks of equal size, each time using only one of them. But when this piece of memory is used up, the surviving object is copied to the other piece, and the memory space that was used is cleaned up once, so that memory fragmentation is not easy.

Not easy to produce memory fragmentation, the drawbacks are obvious, memory utilization is too low, if there are many surviving objects, the efficiency will be very low.

Mark-compact (labeling collation) algorithm

In order to solve the disadvantage of copying algorithm, the labeling algorithm is proposed. The algorithm tag phase is the same as mark-sweep, but after the target tag is completed, instead of cleaning the recyclable object directly, the surviving object is moved to one end and the memory outside the end boundary is cleared.

More ideal algorithm.

Generational Collection (generational collection) algorithm

Mainstream algorithms. The core idea is to divide the memory into several different regions based on the life cycle of the object's survival. In general, the heap is divided into the old age (tenured Generation) and the Cenozoic (young Generation). In the old age, only a small number of objects need to be recycled per garbage collection, and a large number of objects need to be reclaimed each time the new generation is garbage collected.

The new generation generally adopts copying algorithm. And it's not actually dividing the Cenozoic space by a 1:1 ratio. Instead, it is divided into a larger Eden space and two smaller survivor spaces, each time using Eden space and one of the survivor spaces in it, and when recycled, the objects that survived in Eden Space and survivor are copied into another survivor space. Then clean out the Eden space and the survivor space you just used.

The old age generally uses the mark-compact algorithm.

Methods related to Java garbage collection System.GC ()

You System.gc() can request garbage collection using whatever garbage collection algorithm the JVM is using. But invoking a System.gc() garbage collection request just to the JVM, after the JVM accepts the message, does not necessarily do garbage collection at once, but instead weights several garbage collection algorithms, making garbage collection more prone to occur, sooner or later, or more recyclable.

Finalize () method

Before the JVM garbage collector collects an object, it generally requires the program to call the appropriate method to free the resource, but without explicitly releasing the resource, Java provides a default mechanism to abort the object freeing the resource, which is the Finalize () method. Its prototype is:

protected void Finalize () throws Throwable

After the Finalize () method returns, garbage collection begins execution.

The reason for using fianlize () is that there are special cases that the garbage collector cannot handle:

    1. The malloc () method of C + + is called in native methods, but the free function is not called.
    2. Open the file resource.

Resources:

Java garbage collection mechanism

Java garbage collection mechanism

Detailed introduction to Java garbage collection mechanism

Java garbage Collection mechanism notes

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.