Http://www.zhihu.com/question/21539353/answer/18596488 & http://www.zhihu.com/question/21663879/answer/19007323? Group_id = 85420565 # comment-50027587
The biggest difference between reference counting and trace-based automatic memory management is that the former only requires local information, while the latter requires global information.
The most basic form of the reference counting method is to associate each managed object with a reference counter, which records the number of times the object is currently referenced, every time a new reference is created to point to this object, the counter is incremented by 1, and every time the reference pointing to this object fails, the counter is reduced by 1. When the counter value drops to 0, the object is considered dead. Each counter only records the local information of the corresponding object-the number of times it is referenced, and does not (or is not required) The Life and Death information of a global object graph. Because only local information is maintained, you do not need to scan the global object graph to identify and release dead objects. However, due to the lack of global object graph information, circular references cannot be processed. More advanced reference counting introduces the concept of "weak reference" to break some known circular references, but that is another topic.
In practice, it is an interesting topic to see where the reference count exists. Attackers can intrude into an object. For example, CPython stores the reference count in the object header (ob_refcnt field of PyObject) of each Python object managed by the automatic memory, or the IUnknown of COM :: addRef ()/Release (); can also be non-intrusive outside the object, such as std: shared_ptr in the C ++ 11 standard library.
Counter Management (auto-increment/auto-increment) may be manually completed, such as the old Objective-C, or the use of COM from C ++, or automatic management, for example, CPython, "hat" of Objective-C, C ++/CX using "automatic reference count" (ARC), std :: shared_ptr and so on. If it can be automatically managed, there must be a set of clear rules to describe the situation in which the next reference will be considered invalid; Take std: shared_ptr as an example, when the Destructor is called (for example, when it leaves the scope) or points to another object, the reference count of the object originally pointed to will be reduced by 1.
Tracing GC is the opposite of reference count. The Global object graph information is required to be scanned from the "root" (that is, the inevitable reference) of the object graph, determines the life and death of an object based on the accessibility of reference. This allows objects to be recognized and released in batches.Tracing GC does not explicitly maintain the reference count of an object. It can only answer "yes" or "no" when Tracing..
Actually,With ample memory, Tracing GC has a lower overall overhead than the reference counting method, so throughput is higher. Because the reference counting method usually requires statistics of redundant local information, and the tracing GC can identify the life and death of objects in one batch through global information; if it is a tracing GC with sorting, the memory allocation is usually faster.
However, tracing GC generally has a higher latency than the reference counting method, and the efficiency of tracing GC is lower when the memory is tight, therefore, it is still a reasonable choice to use the reference count in areas with insufficient memory (for example, the ARC on iOS5 ).
Chapter 6th of The Garbage Collection Handbook provides a detailed comparison of various basic GC methods.
GC-related books I sorted out a book order on Douban, [Garbage Collection] [Garbage Collection] [automatic useless memory unit Collection] related books
Among them, Zookeeper has been installed. This book has analyzed the source code of GC in several languages, which is worth reading. The book is in Japanese, but I am promoting the introduction and translation of it by Chinese publishers. Please look forward to its Chinese version.
The reason why GC in Java and other languages does not release memory in real time is as follows: