What Is Reference Counting?
Reference counting is a memory management technology in computer programming languages. It refers to saving the reference times of resources (which can be objects, memory, disk space, etc.), and releasing them when the reference times become zero. process. The use of reference counting technology can achieve the purpose of automatic resource management. At the same time, reference counting can also refer to a garbage collection algorithm that uses reference counting technology to recover unused resources.
- The most intuitive garbage collection strategy is reference counting. Reference counting is simple, but requires
- Compared with tracked garbage collection, the main advantage of reference counting is that objects that are no longer being used can be recycled as quickly as possible, at the same time it will not cause long pauses in the recycling process, and it can clearly indicate the life cycle of each object.
- In real-time applications or memory-constrained systems, real-time responsiveness is an important indicator, and reference counting is one of the easiest garbage collection technologies to implement, which is suitable for this situation. Reference counting can also be used to manage other non-memory resources, such as operating system objects (often more scarce than memory resources). Tracked garbage collection uses a finalizer to handle such targets, but delayed collection can cause other problems. Weighted reference counting is a derivation technique suitable for distributed systems.
- On platforms where available memory is full of active objects, traceable garbage collection is frequently triggered, reducing performance. And reference counting performance is guaranteed even when the memory is nearly exhausted. Reference counting can also provide reference information for other runtime optimization techniques. For example, for many systems that use immutable objects (such as functional programming languages), the performance penalty caused by the large number of copied objects is sometimes severe; on such systems, a Typical optimization measures are: if an object is used only once after it is created, and another similar object is created at the same time that it is no longer referenced (such as the string concatenation assignment operation in Javascript), the original can be deleted The object's behavior of creating a new object becomes modifying the original object, thereby improving efficiency. Reference counting can provide sufficient reference information for such optimizations. [1]
- Unoptimized reference counting has two major disadvantages compared to tracked garbage collection, and both need to be fixed by introducing additional mechanisms:
- Frequently updating the reference count can reduce operational efficiency.
- Raw reference counting does not resolve circular references.
- In addition, if the free list is used to allocate memory, the spatial locality of the reference count is very poor. Using reference counting alone cannot improve the performance of the CPU cache by moving objects, so high-performance memory allocators will also implement a tracking garbage collector to improve performance. Many reference counting implementations (such as PHP and Objective-C) perform poorly because memory copies are not implemented. [2]