Details of Java garbage collection __ garbage collection

Source: Internet
Author: User

We all know the Java garbage collection mechanism, Java has its own garbage collector to automatically recycle garbage. I have been aware of the garbage collection mechanism before the Java garbage collector is automatically recycled garbage, there are two ways to know about finalize and system, and people tell me that you know that Java virtual machines have garbage collection. You don't have to worry about that. I have always believed, and lamented the Java virtual machine really magical. It wasn't that much of a problem until my understanding of Java began to deepen.

Here is a little story: there is a small tyrants home to ask a nanny to clean every day, but do not know when the nanny will come; the Java garbage collector is the same as the nanny we invited, it cleans up the rubbish but you can't control when he's going to clean it up.

We have to figure out the following three questions about the recycling mechanism:

1. What kind of rubbish needs to be recycled.

2. When to do garbage collection.

3. How to recycle the rubbish.

1 How to judge the object into garbage.

Java has an algorithm called a accessibility algorithm, the idea is to a "GC Roots" object as the starting point, from these nodes down search, the search path is called a reference chain, when an object to the GC Roots is not linked to any reference chain (that is, from GC Roots To this object, you can prove that the object is not available and will be used as a garbage collection object.

GC Roots objects that are not pointed to search for objects to be reclaimed the process of having at least two marks at a time when any object is garbage collected.

GC roots is marked once when it is not pointing to the object, which is not actually reclaimed when it is marked this time.

2. When to do garbage collection.

After marking the first time, it is determined whether the object is necessary to execute the Finalize () method, and the Finalize method will mark the object for the second time, and the object will surely be reclaimed by the garbage collector once it is marked for a second time.

If necessary, the objects are placed in a queue called F-queue, and then the virtual opportunity automatically establishes a lower-priority thread to perform garbage collection, so it can be judged that the garbage collector is garbage collected when the program is idle.

Here is the Finalize method, which belongs to the object class and is itself an empty method. Special case if the object in finalize points to a class variable, the object will escape a robbery and will not be garbage collected.

I don't remember where I saw that. You can release program resources in the Finalize method. This approach is completely undesirable because the code that frees the resources is performed much better in a finally code block because the finally code block is controlled by the programmer and the Finalize method is not controlled by the programmer.

3. How to recycle the rubbish.

Run the Finalize method, and then start the garbage collection.

The garbage collection of the currently used virtual machines uses a generational collection algorithm that divides the memory into several pieces based on the different inventory cycles of the objects. Java heap is divided into: the Cenozoic and the old age, this can be based on the characteristics of various eras to adopt the most appropriate collection algorithm;

In the new generation of garbage collection every time there are a large number of objects die, only a small number of survival, then choose the replication algorithm for recycling;

In the old age, because of the high survival rate of the object, there is no extra space to allocate it to guarantee, it must use the tag algorithm to recycle;

Replication: The memory is divided into two equal size, each time only use one of the pieces, when this piece of memory used up, will still survive the object to copy to another piece of storage space, and then the use of the memory space to clean out all at once.

(It can also be judged by this: garbage collection occurs when the memory is almost full)

Marking: Divided into Mark---elimination algorithm and tag---finishing algorithm

Mark clear: Mark all objects to be reclaimed, and then unify the collection; This is inefficient and produces a lot of fragmented memory fragmentation, which is rarely used;

Marking: Mark all objects to be recycled, then let all the surviving objects move toward one end, then clean the other end of the garbage directly;

The garbage collection process also carried out in-memory debris cleanup work;

If there is a memory is not particularly understanding of the small partners can be another way to understand:

Everything that the master doesn't have is rubbish. Host here as a GC Roots object He doesn't want to be associated with something, and these things naturally turn into rubbish.

The nanny searched all the items in the house and saw that the new drink had not been opened. See fresh oranges also exudes fresh fragrance, as the garbage collector's nanny is very clear that these things are new do not need garbage collection, but the nanny will be finished drink bottles, after eating the remaining orange peel collected, (Beverage bottles can be filled with water, orange peel can also make tea, and reuse function is like GC roots objects that cannot be pointed back to the other class object's properties will be activated again) ask if the owner is really going to get rid of these things. If the owner says yes, execute the Finalize method, Mark two times, and do the garbage cleanup at a certain time period.

When there are many rubbish, put everything in a bag, take out a few things that are not lost, and then throw away the whole bag, which is called the duplicate.

When the litter is small, it also puts everything in a bag and throws out a few discarded rubbish, which is called marker.


1.GC Roots objects that are not pointing are searched for objects to be reclaimed.

2. The garbage collector makes garbage collection when the program is idle. Garbage collection is done when the memory is full.

3. The implementation of the Finalize method will start garbage collection, the new generation of replication-type recycling, the old era of tagged recycling.

See here everyone should be a little understand why pay attention to garbage recycling details of it.

1. Because the thread priority for the execution of the garbage collection method is low, the garbage collector will force garbage collection if the new object is scrambled and the memory space is nearly full.

When the garbage collector is working to consume system resources, it will inevitably affect the operation of other threads and affect the efficiency of the program.

2. Let the programmer have the vigilance consciousness, can use StringBuffer to modify the character not to use string to create several objects to splice and Occupy memory.

can use person P = new person (); To invoke a method by referencing the variable p, do not invoke it with the anonymous object new person ().

3. With the increase in the number of visits to the program, the bottom of the program is sure to optimize, memory space is also a very important place ~

Related Article

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