Вы находитесь на странице: 1из 3

Reference counting

Basic idea:
add an extra integer (the reference count) to
every heap-allocated data structure
when first allocated, initialize the reference
count to 1

Reference Counting

whenever a new reference to the data structure


is established, increment the reference count
whenever a reference disappears, decrement the
reference count and check if it is zero
if it is zero, then reclaim the storage

Reference counting example


var
var
var
l =

l: intlist*;
m: intlist*;
n: intlist*;
alloc();

if () {
m = l;

} else {
n = alloc();
l->next = n;

}
l = alloc();

1/ 2

1
/2

null

null

Reference counting example


var
var
var
l =

l: intlist*;
m: intlist*;
n: intlist*;
alloc();

if () {
m = l;

} else {
n = alloc();
l->next = n;

}
l = alloc();

1 1/ 2
/

1
/2

null

null

null

RC is nave

RC is broken

Reference counting has been (re)invented


many, many times

More importantly, for most programming


language applications, reference counting
doesnt work well for cyclic structures.

It is often the first idea that comes to mind for


dynamic memory management and has the
advantage of simplicity
also real-time behavior
also, objects stay in one place

several techniques for this, but hard and messy

However, it is inefficient
in storage, because of the reference counts, and
in speed, because of the need always to maintain
the counts

Advantages:The main advantage of the reference counting over tracing garbage collection is that objects are reclaimed as
soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection
cycles and with clearly defined lifetime of every object.
In real-time applications or systems with limited memory, this is important to maintain responsiveness.
Reference counting is also among the simplest forms of memory management to implement. It also allows for
effective management of non-memory resources such as operating system objects, which are often much scarcer
than memory (tracing GC systems use finalizers for this[citation needed], but the delayed reclamation may cause
problems). Weighted reference counts are a good solution for garbage collecting a distributed system.
Reference counts are also useful information to use as input to other runtime optimizations.

Disadvantages:The frequent updates it involves are a source of inefficiency. While tracing garbage collectors can impact
efficiency severely via context switching and cache line faults, they collect relatively infrequently, while
accessing objects is done continually. Also, less importantly, reference counting requires every memory
managed object to reserve space for a reference count. In tracing garbage collectors, this information is
stored implicitly in the references that refer to that object, saving space, although tracing garbage collectors,
particularly incremental
ones, can require additional space for other purposes.
The naive algorithm described above can't handle reference cycles, an object which refers directly or
indirectly to itself. A mechanism relying purely on reference counts will never consider cyclic chains of
objects for deletion, since their reference count is guaranteed to stay nonzero. Methods for dealing with this
issue exist but can also increase the overhead and complexity of reference counting on the other hand,
these methods need only be applied to data that might form cycles, often a small subset of all data. One
such method is the use of weak references, while another involves using a mark sweep algorithm that gets
called infrequently to clean up.

Вам также может понравиться