The reference counter method of garbage collector algorithm

Source: Internet
Author: User
Tags garbage collection

The reference counter method of garbage collector algorithm

Microsoft will run the code in the common language runtime into managed code, but in a broader sense, as long as the language provides automatic memory management capabilities, we use its developed code can be called managed code, automatic memory management is what we call garbage collector, garbage collector implementation is a complex process, It involves a lot of detail; the difficulty of the garbage collector is not the garbage collection process, but the location of the garbage object. When an object is no longer referenced, it can be recycled, but how do we know it's not being referenced?

algorithm Definition

Adds a field for 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, as shown in the following figure


As we can see from the above figure, each assignment of a reference type requires a runtime update counter, and the runtime's update code might look like this

if (P! = q)

{

if (P! = null)

--p.refcount;

p = q;

if (P! = null)

++p.refcount;

}

One of the advantages of the counter algorithm is not to wait for the memory is not enough, the garbage collection, which can be in the assignment operation while checking whether the counter is 0, if yes, can be recovered immediately; The code at runtime may be as follows

if (P! = q)

{

if (P! = null)

if (--p.refcount = = 0)

Heap. Release (P);

p = q;

if (P! = null)

++p.refcount;

}

One of the drawbacks of the counter algorithm is that it cannot solve the problem of circular references, as shown in the following diagram, we construct a list of the last element's next property, which refers to the first element, which is the reference to the first element, thus constituting a circular reference; this time if we assign the header head of the list to NULL, The counters for each element of the list are not 0, and we also lose the control of the reference to the list, causing the list elements to not be recycled.

algorithm features

1. A separate field storage counter is required to increase the overhead of storage space;

2. Each assignment needs to update the counter, increasing the time overhead;

3. The garbage object is easy to identify, as long as the counter is 0, it can be used as garbage collection;

4. Timely recovery of waste, no delay;

5. The problem of circular references cannot be resolved;

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.