Headline

Reference counting is a Memory management technique that keeps a counter of how many times an object is referenced. When the count reaches zero, the memory is freed.

Illustration

The mechanism can easily be illustrated by the following Language:Perl script, which peeks at the internal reference count of a variable:

#!/usr/bin/perl
use feature     'say';
use Devel::Peek 'SvREFCNT'; # access to reference count

my $thing = 'stuff';        # create a thing
say SvREFCNT($thing);       # refcount = 1

my $ref1  = \$thing;        # create reference to thing
say SvREFCNT($thing);       # refcount = 2

{                           # begin new block
    my $ref2 = \$thing;     # create another reference
    say SvREFCNT($thing);   # refcount = 3
}                           # code block ends, $ref2 goes out of scope
say SvREFCNT($thing);       # refcount = 2

$ref1 = 'something else';   # remove a reference
say SvREFCNT($thing);       # refcount = 1

This approach is simple to implement (almost trivial even when using a language with RAII) and destruction happens deterministically. However, circular references can still lead to Memory leaks, which must either be avoided or explicitly marked weak references.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.