A garbage collection, reference counting method to maintain the & all object

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The original link: &&

Based on reference counting and trace these two categories of automatic memory management is based on the biggest difference is: the former requires only local information, while the latter requires global information.

Reference counting is the basic form of the way to let each managed object and a reference counter associated together, the counter records the number of the object currently referenced, and create a new reference to the object when the counter is 1, when the reference to the object failed counter is minus 1. When the counter value is reduced to 0 that the object of death. Each counter records only local information &mdash the corresponding object; — citation counts, not (need not) a global object graph and information. Due to maintain only local information, so there is no need to scan the global object graph can be identified and released dead object; but also because of the lack of global object graph information, so can not handle the circular reference condition. More advanced reference counting implementation will introduce “ weak references ” concept to break certain known circular references, but that is another topic.

In fact, the reference count is what place is an interesting topic. Can invade object exists, for example CPython the reference count in each of the automatic memory management of Python object header (ob_refcnt field PyObject), or COM (IUnknown:: AddRef) (/Release); can also be non intrusive presence outside the object, such as the C++11 standard library: shared_ptr std:.

Counter management (auto increment / decrement) may be completed by manual, such as the old Objective-C, or from C++ using COM, and so on; may also be automatic management, such as CPython, use the “ automatic reference counting (” ARC) Objective-C, C++/CX “ hat”, the previously mentioned C++11 std:: shared_ptr. If the automatic management, then there must be a clear set of rules that would have been considered invalid reference conditions under which a; std:: shared_ptr as an example, the destructor is called (for example goes out of scope) or point to other objects, the original point to the object's reference count will decrease 1.

Tracing GC and the reference count on the contrary, the need for a global object graph information, from the object graph of “ ” (which is bound to live reference) starting scan out, citing reachability to determine the object based on the life and death. This makes the object can only be identified birth mass death state and release the dead objects. Tracing GC do not explicitly maintain the object's reference count, only in the trace time to answer “ ” or “ no ” live a reference to an object.

In fact, in the premise of memory plenty of tracing GC, the overall overhead than the reference count is lower, so the throughput (throughput) high. Because the local information reference counting methods usually require statistical redundancy, while the tracing GC is available through the global information breath volume to determine object of life and death; if it is with a finishing tracing GC, then the memory allocation will usually be faster.

But tracing GC often than reference delay counter mode (latency) is big, and the efficiency of tracing GC memory more nervous but lower in memory, so not too abundant place using reference counting is still a reasonable choice (e.g. iOS5 ARC).

The Garbage Collection Handbook the sixth chapter detailed comparison of basic GC modes, there is not much.

The GC book I put a book on douban.com, [Garbage Collection][garbage collection. The automatic useless memory unit recovery] related books

Among them, and image studies Danish and,,, and of rubber and mounting the book has some language in the GC source code analysis, worth reading. The book is in Japanese, but I have in promoting the domestic press introducing and translating it. Please come forward to the Chinese version of the.


The reason for languages such as Java GC not in time to release the memory, the key points in the:

    1. The most basic of pure reference counting method of automatic memory management can achieve real-time release dead objects, but cannot handle circular references exist to release the object graph. This problem to a certain extent, can be solved by introducing the concept of weak references, but common to deal with the circular reference reference counting objects graphs are backup other management mode (usually a tracing GC, such as mark-sweep), such as CPython uses a reference counting, mark-sweep supplemented by the way, Adobe Flash ActionScript VM 2 (AVM2) is to delay the reference count (DRC), incremental / conservative assisted by mark-sweep. On the other hand, like the C++ std:: shared_ptr is the pure reference counting, cannot rely on to deal with the circular reference object graph, and must rely on the programmer used carefully, where necessary using std:: weak_ptr to break the cycle; CPython before 2 also use pure reference counting, unable to handle the circular reference, can only wait for leakage memory. Since the general reference counting can use tracing GC to backup, automatic memory management that is equal to achieve two, think lazy as a start implementing only a tracing GC, such as mark-sweep.
    2. The most basic of pure reference counting on the reference counter operation is very frequent, there are additional overhead, whether serious to a tolerable level depends on the specific application. based on memory ample, the basic tracing GC than the basic reference counting means better performance (especially from the perspective of throughput), without the need for redundant counter update. At the same time, the reference counter may be the sharing of data between threads in a multithreaded environment, need to do synchronous protection (here an atomic update is synchronous protection), which is the source of a overhead; because tracing GC is not required to maintain the reference counter so there is no such synchronization overhead. Refer to the performance disadvantages count could be relieved by some senior variants, such as the aforementioned delay AVM2 reference counting, recording only between heap object's reference count without recording stack (mainly is the expression temporary value) of an object's reference count, in order to reduce the number of updates to the counter to improve performance. Please refer to the document: MMgc | MDN. These reference counts senior variants usually means a delayed release to a certain extent, with the landlord to real-time release's original intention is a discrepancy. On the other hand, although the most basic tracing GC delays longer, but they also have advanced varieties, can be parallel, concurrent, incremental implementation, reducing the delay; also the way to achieve thread-local GC to deal with such as &ldquo request response; ” type Web application batch release object a temporary assignment thread the demand.
    3. If you use tracing GC to realize the automatic memory management, it is not explicitly maintain the object's reference count, there is no reference count to “ 0” concept. So the natural environment is not &ldquo tracing GC JVM or other language runtime based on reference counting to 0; release the object”.
    4. The reference count is also the classic cotton situation. One example is an object a number, reference chain long object graph if only by a reference to stay alive, so that reference a death will lead to a large object shot release (but not “ batch release ”, overhead), this will lead to cotton. Simple discussion the worst case actually also has a reference count so bad. Pure manual (/free) or (malloc) new/delete allows programmers to flesh out the same object life cycle, then use such as arena way to allocate memory for them, can they die the real batch release it, so it is efficient; but the pure reference counting is not such a thing. Using reference counting will encounter this kind of cotton the object graph references in your code is what.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Diana at November 12, 2013 - 12:05 PM