3
 Reference Counting 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 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 l: intlist*; var m: intlist*; var n: intlist*; l = alloc(…); if (…) {  m = l;  } else {  n = alloc(…);  l->next = n;  } l = alloc(…); 1 null 1 null  / 2  / 2  Reference counting example var l: intlist*; var m: intlist*; var n: intlist*; l = alloc(…); if (…) {  m = l;  } else {  n = alloc(…);  l->next = n;  } l = alloc(…); 1 null 1 null  / 2  / 2 1 / 1 null

Reference Counter123

Embed Size (px)

DESCRIPTION

Reference Counter

Citation preview

  • Reference Counting

    Reference counting

    Basic idea:

    add an extra integer (the reference count) toevery heap-allocated data structure

    when first allocated, initialize the referencecount to 1

    whenever a new reference to the data structureis established, increment the reference count

    whenever a reference disappears, decrement thereference count and check if it is zero

    if it is zero, then reclaim the storage

    Reference counting example

    var l: intlist*;

    var m: intlist*;

    var n: intlist*;

    l = alloc();

    if () {

    m = l;

    } else {

    n = alloc();

    l->next = n;

    }

    l = alloc();

    1 null

    1 null

    / 2

    / 2

    Reference counting example

    var l: intlist*;

    var m: intlist*;

    var n: intlist*;

    l = alloc();

    if () {

    m = l;

    } else {

    n = alloc();

    l->next = n;

    }

    l = alloc();

    1 null

    1 null

    / 2

    / 2

    1 /

    1 null

  • RC is nave

    Reference counting has been (re)inventedmany, many times

    It is often the first idea that comes to mind fordynamic memory management and has theadvantage of simplicity

    also real-time behavior

    also, objects stay in one place

    However, it is inefficient

    in storage, because of the reference counts, and

    in speed, because of the need always to maintainthe counts

    RC is broken

    More importantly, for most programminglanguage applications, reference countingdoesnt work well for cyclic structures.

    several techniques for this, but hard and messy

    2

    1 1

    x

    jrfTypewritten textAdvantages:-

    jrfTypewritten textThe 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.

    jrfTypewritten textIn 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.

    jrfTypewritten textReference counts are also useful information to use as input to other runtime optimizations.

  • jrfTypewritten textDisadvantages:-

    jrfTypewritten text

    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 memorymanaged 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.

    jrfTypewritten textThe 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.