Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Unlike some other languages, Perl performs automatic garbage collection. Say you've defined a variable my $str = 'Hello world' then that needs to be stored in memory somewhere. When the variable goes out of scope, and Perl knows that it's not going to be used again, Perl's garbage collector can free the area of memory it was being stored in (so that the memory can be used for storing something else).

In some cases you end up with circular references:

my $a = []; my $b = [ $a ]; push @$a, $b; # Now $b contains a reference to $a and # $a contains a reference to $b.

OK, that's a contrived example, but circular references do come up in real code all the time:

sub marry { my ($husband, $wife) = @_; $husband->set_spouse($wife); $wife->set_spouse($husband); $wife->parent[0]->pay_for_party; }

Now perhaps the $wife object contains a reference to $husband and vice versa.

In cases like this, Perl's garbage collector perhaps considers freeing the memory used by $wife but sees that $wife is still being referred to by $husband, so it can't free that memory until $husband is destroyed. But when it considers freeing the memory used by $husband it sees that $husband is being referred to be $wife, so it can't free anything there either.

Technically the way Perl does this is by maintaining a "reference count" for each thing that's in memory - in other words, a count of how many variables and references are pointing at it.

This catch-22 situation prevents either of the objects being freed from memory until the program exits. In short-running programs, this is often not a problem. But for long-running processes, it results in a memory leak - the program's memory consumption grows and grows over time.

weaken (found in the Scalar::Util package, which has been bundled with Perl seince 5.7.3) breaks the catch-22. It allows you to mark a reference as "weak" in which case it won't count towards the reference count.

As somebody else pointed out, in the original code (but not your cut-down example), $wrapper forms a closure over itself, which is a type of circular reference. This will result in memory leaking each time once is called. (Though judging by the function name, it is presumably only called once?) Thus weaken is used to break the circularity (as far as the garbage collector is concerned), and prevent a memory leak.

In reply to Re: What's the weaken here for? by tobyink
in thread What's the weaken here for? by PerlOnTheWay

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2017-11-25 04:08 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (355 votes). Check out past polls.