Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Perl Garbage Collection, again

by pkirsch (Novice)
on Dec 31, 2008 at 12:11 UTC ( [id://733453]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl Garbage Collection, again
in thread Perl Garbage Collection, again

Helpful, indeed.
What I recognized:
- allocating a big chunk of memory at once is evil (for releasing it afterwards):
use strict; $| = 1; print "$$\n"; #top -p $$ print "Test, Allocating a large string \n"; <>; { my $foo = 'X' x 100000000; print "Large String allocated.\n";<>; undef $foo; print "Large String deallocated.\n";<>; } print "2nd Large String.\n";<>; { #evil: my $foo2 = 'X' x 100000000; my $foo2; $foo2 .= 'x' x 1000 for (1 .. 100000); print "2nd Large String allocated.\n";<>; undef $foo2; print "2nd Large String deallocated.\n";<>; } print "Now what? Press enter to exit"; <>;
As you can see the memory used for $foo2 is returned to the OS. So my initial example was not correct.

May I ask you another question, which puzzles me:
The above example also shows that at the end of the of script there are still 97m allocated (due to $foo). Also, the variable $foo2 does not use the previous allocated chunks of $foo (because the usage also rises up to total usage 192m.
Is the caused by the overhead of the internal memory handling of perl?
Thanks,

Replies are listed 'Best First'.
Re^3: Perl Garbage Collection, again
by BrowserUk (Patriarch) on Jan 02, 2009 at 15:01 UTC

    Sorry for the delay. I got into something else and overlooked this. Try this version of your code:

    use strict; $| = 1; print "$$\n"; #top -p $$ print "Test, Allocating a large string \n"; <>; { my $foo = 'X'; $foo x= 100000000; print "Large String allocated.\n";<>; undef $foo; print "Large String deallocated.\n";<>; } print "2nd Large String.\n";<>; { #evil: my $foo2 = 'X' x 100000000; my $foo2; $foo2 .= 'x' x 100_000 for (1 .. 1000); print "2nd Large String allocated.\n";<>; undef $foo2; print "2nd Large String deallocated.\n";<>; } print "Now what? Press enter to exit"; <>;

    When that reaches the "Now what" prompt, you should find that the memory usage has return to the same level as you had at statrtup with all the large allocations now returned to the OS.

    The main change is my $foo = 'X'; $foo x= 100_000_000;, rather than my $foo = 'X' x 100_000_000;.

    With the latter version, the big string is constructed on the stack, and then assigned to the scalar $foo, with the result that it makes two large memory allocations, one of which never gets cleaned up.

    With the former version, that double allocation is avoided and the memory is cleaned up properly.

    Note. The minor change to the second loop 1_000x100_000 rather than 100_000x1_000 makes no difference to the outcome, I just got bored waiting for the loop to run :)

    The duplication of the allocation using the second method isn't an error, but rather a side effect of the way the code is parsed and executed. The fact that it doesn't get cleaned up properly could be construed as a bug--or not. You'd have to raise the issue with p5p and take their view on the matter.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://733453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found