Hi Monks, I'm trying to understand the garbage collector. Please have a look at my code example:
#!/usr/bin/perl
use strict;
use warnings;
package Foo;
sub new {
my $self = bless { }, shift;
for (1..2_000_000) {
$self->{x} .= "x" x 1000;
}
return $self;
}
package Bar;
sub new {
my $self = bless { }, shift;
for (1..2_000_000) {
$self->{$_} = "x" x 1000;
}
return $self;
}
package main;
print "### Test Foo\n";
my $foo = Foo->new;
print "Object created\n";
print "Size: ", qx{ps -p $$ -o rss=};
$foo = undef;
sleep 3;
print "Object destroyed\n";
print "Size: ", qx{ps -p $$ -o rss=};
print "### Test Bar\n";
my $bar = Bar->new;
print "Object created\n";
print "Size: ", qx{ps -p $$ -o rss=};
$bar = undef;
sleep 3;
print "Object destroyed\n";
print "Size: ", qx{ps -p $$ -o rss=};
Output:
### Test Foo
Object created
Size: 1955472
Object destroyed
Size: 2348
### Test Bar
Object created
Size: 2647552
Object destroyed
Size: 2631164
The Foo example is that what I expect. But I don't understand why the memory is not releases back to the operating system in the Bar example.
Could someone bring some light into my darkness?
Cheers,
Jonny
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|