This very simple and seemingly trivial task of illustrating perl's garbage collection seems to exhibit the most unexpected results and I'm clueless as to why. One would expect to find a reduction in the RSS of the process when a huge hash is deallocated, undef'd *and* goes out of scope but far from decreasing, the RSS seems to have increased in the end.
Why isn't it being garbage collected when it goes out of scope? When exactly is the hash garbage collected by perl? If the answer is when the program exits, then it seems like we'd have bigger problems when dealing with long running processes such as mod_perl.
One plausible explanation could be that even though perl actually garbage collects it, it's not released to the OS to be reclaimed just yet. If that is indeed the case then it seems like long running tasks/processes like mod_perl continually grow in size until that particular apache/mod_perl http process is killed/terminated.
What am I missing here? Any insights appreciated.
perl -e '
use strict;
use warnings;
print "\nInitial size:\n" . qx {ps -o rss $$};
{
my %x = ();
for (my $i = 0; $i < 100000; $i++) {
$x{$i} = 1;
}
print "\nafter allocating a huge hash:\n" . qx {ps -o rss $$};
for my $k (keys %x) {
delete $x{$k}
}
undef %x;
print "\nafter deallocating the huge hash\n" . qx {ps -o rss $$};
}
print "\nafter the huge hash goes out of scope\n" . qx {ps -o rss $$};
+'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.