Garbage collection mechanism in Java

Source: Internet
Author: User

1, reference counting method: Old garbage collection algorithm, by reference count to recycle garbage; Adds a field to each object to record the number of times it is referenced, and the total number of references that are tracked and updated by the runtime;

Object p = new ComparableInt32 (57);

Object q = P; We instantiate an object ComparableInt32 and assign it to the variable p, at which point P refers to the object, so its counter is 1, then we assign a value to the variable Q with P, and q also references the variable, so its counter becomes 2;

Disadvantage: Cannot solve the problem of circular reference, need separate field storage counter, increase the space cost, each assignment should be updated, increase the time overhead.

2, Mark-Clear algorithm: Is the modern garbage collection algorithm ideological foundation. The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase. A feasible implementation is to mark all the objects that can be reached from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared.

When using Mark Rub division, the garbage object can not be collected immediately, but the garbage collection is only triggered when the memory is not enough to use; this time the program execution process will be temporarily dormant, once all garbage collection, will wake up the normal program execution process. Mark Wipe Division is also known as the garbage collector algorithm because it tracks all objects that can be accessed directly or indirectly by the program, which can be accessed directly by the program, with local variables and static variables. In the collector, these variables are called root objects. An object referenced by another object field is called an object that can be accessed indirectly. So you can normally access the object that we call the surviving object, otherwise it is garbage.

The Mark Erase algorithm consists of two stages, at the first node it first finds all accessible objects and marks them, and this stage is called the tagging phase, and the second stage is to scan all unmarked objects on the stack and recycle the memory operations, which is called the erase node.

To distinguish between garbage objects and normal objects, we need to record the state of each object, so we can add a Boolean type of field marked to each object. By default, all objects are not marked when they are created, so the initial value of the field marked is false;

By marking the given object P and all the objects that can be accessed indirectly through P, we can do this by using a method similar to the following mark:

Void Mark (Object p)

{

If (!p.marked)

{

P.marked = true;

For each object, q referenced by P

Mark (q);

}

}

We can notice that at this stage of the mark, when we encounter objects that have already been tagged, nothing is done. Therefore, the algorithm can be guaranteed to end normally, and only if all accessible objects are marked.

In the second phase, the algorithm scans all the objects on the heap to locate all unmarked objects. During the scan, the memory occupied by these unlabeled objects is recycled, and the marked field of each normal object is reset to false to prepare for the next collection.

void Sweep ()

{

For each object p in the heap

if (p.marked)

P.marked = False

Else

Heap. Release (P);

}

Because Mark Rub Division keeps track of all accessible objects through the root object, it is possible to correctly identify and reclaim garbage objects even in the case of circular references. This is the biggest advantage of its relative counting method. However, the disadvantage is that when the algorithm is executed, the normal execution process of the dormant interrupt program is needed, especially the system which needs human-computer interaction and needs to meet the demanding real-time execution requirements. Another problem is a memory fragmentation problem. It often occurs in long-running systems that have been running several times in the garbage collector. The concrete embodiment is that the normal object is isolated by a lot of unused small memory fragments, which may cause the available memory to satisfy the requested memory, but because these memory is not contiguous, causing the problem of not allocating memory properly.

3, marker-compression algorithm: Suitable for the survival of more occasions, such as the old age. It does some optimizations based on the mark-and-sweep algorithm. Like the mark-and-sweep algorithm, the tag-compression algorithm first needs to start with the root node and mark all objects that can be reached. However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory. After that, clean up all the space outside the boundary.

4. Copy algorithm: Divide the original memory space into two pieces, use only one piece at a time, during garbage collection, copy the surviving objects in the memory in use into the unused memory block, then clear all objects in the memory block in use, swap the two memory roles, complete the garbage collection

Disadvantage: not suitable for the existence of more occasions such as the old age; Waste space

5. Combining the idea of tag cleaning with the new algorithm proposed by the replication algorithm:

Divides the memory into several blocks depending on the life cycle of the object. In general, the Java heap is divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm. In the new generation, each garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects copy cost can be completed collection. In the old age, because of the high survival rate of the object and the lack of additional space to guarantee his distribution, it must be recycled using the "mark-and-tidy" algorithm.

Reference: http://blog.csdn.net/hou478410969/article/details/7533445

http://blog.csdn.net/java2000_wl/article/details/8022293

Garbage collection mechanism in Java

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.