bloonix has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I'm trying to understand the garbage collector. Please have a look at my code example:
Output:#!/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=};
### 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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Memory is not released back to operating system
by educated_foo (Vicar) on Mar 02, 2012 at 18:33 UTC | |
by Argel (Prior) on Mar 02, 2012 at 21:48 UTC | |
by eyepopslikeamosquito (Bishop) on Mar 02, 2012 at 23:13 UTC | |
Re: Memory is not released back to operating system
by zentara (Archbishop) on Mar 02, 2012 at 19:27 UTC | |
by bloonix (Monk) on Mar 03, 2012 at 00:22 UTC | |
by shawnhcorey (Friar) on Mar 03, 2012 at 16:49 UTC | |
Re: Memory is not released back to operating system
by JavaFan (Canon) on Mar 02, 2012 at 20:33 UTC |
Back to
Seekers of Perl Wisdom