Hi All,

I'm now working on a daemon and am trying to improve garbage collection. Until now, I was under the impression that collection was completely automatic, but this quick test says otherwise (on Linux):

#!/usr/bin/perl use strict; use warnings; show_size(); { my @var=(0..1000000); show_size(); } show_size(); exit(0); sub show_size { local $/; open(my $pfh, '<', "/proc/$$/status") || die $!; my $size = <$pfh> =~ /VmSize:\s+(\d+)/ ? $1 : 'unknown'; close($pfh); print "Process size: $size\n"; } # Output is # Process size: 49956 # Process size: 53864 # Process size: 53864

But, if I add in some explicit undefs, the result changes:

#!/usr/bin/perl use strict; use warnings; show_size(); { my @var=(0..1000000); show_size(); undef @var; } show_size(); exit(0); sub show_size { local $/; open(my $pfh, '<', "/proc/$$/status") || die $!; my $size = <$pfh> =~ /VmSize:\s+(\d+)/ ? $1 : 'unknown'; close($pfh); print "Process size: $size\n"; undef $pfh; undef $size; } # Output is # Process size: 49660 # Process size: 53568 # Process size: 49660

I was under the impression that when a variable went out of scope it would get cleaned up by Perl's automatic garbage collection. But if that's the case, why does the process size not go down after @var falls out of scope without the explicit undef? Am I misunderstanding how garbage collection works? A colleague says that Perl will free up the memory for re-use, but won't let it go. Is this how it really works?

Either way, what is the best practice for controlling memory use in a daemon setup? Any insights appreciated.

cLive ;-)


In reply to Understanding garbage collection specifics... by cLive ;-)

Title:
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, details, 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, summary, 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.