Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Tracking down memory leaks

by Joost (Canon)
on Apr 13, 2005 at 12:59 UTC ( [id://447354]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tracking down memory leaks
in thread Tracking down memory leaks

Shouldn't these situations be handled by garbage collection.
Maybe, but mostly they aren't, for performance reasons:
sub bla { my $arg = shift; my $big_string = $arg x 1000; }

Perl will in general keep the memory for $big_string allocated and reserved, because then it doesn't need to allocate the memory again next time the sub is called.

Explicitly undef()ing or resizing variables before they go out of scope sometimes helps, though - on some systems, it might even free the memory back to the system.

Usually, you don't need to do this, exactly because the memory gets reused anyway. If your program grows a lot, it's more likely you're using an inefficient algorithm, or you're creating circular references somewhere.

Replies are listed 'Best First'.
Re^4: Tracking down memory leaks
by Hena (Friar) on Apr 13, 2005 at 13:22 UTC
    So basicly if one has a large variable, which should be removed by scoping (not counting the speed usage but memory in should word), one should do manually. But if the large variable is within sub and that sub is used again (eg from a loop structure), then all the variables within it is automatically reused.
      That's how I understand it, yes. Lexicals are re-used when iterating though a loop / subroutine note: recursive subroutine calls create a "stack" of lexicals - for obvious reasons - lookup goto &sub if you want to do tail recursion.

      Ofcourse, if your program is structured well enough, you usually don't have to worry about it - what I mean is, if your variables go out of scope, you usually are going out of a sub or loop in which they will be used again.

      The usual exception is some code that runs in stages, and you want to free as much memory as possible after you're done with one stage and enter the next, for instance, some code that needs to preprocess a lot of data in memory, write it out to a file and then go through the resulting file line by line - or something like that.

Re^4: Tracking down memory leaks
by Anonymous Monk on Apr 13, 2005 at 15:09 UTC
    Note that in your example, Perl will keep the memory for $big_string twice:
    #!/usr/bin/perl use strict; use warnings; use Readonly; Readonly my $size => 10 * 1024 ** 2; # 10 Mb. sub show_mem { system "grep ^VmSize /proc/$$/status"; } sub gimme_big { my $size = shift; my $var = 'x' x $size; } show_mem; gimme_big $size; show_mem; __END__ VmSize: 3464 kB VmSize: 23952 kB
    For a 10Mb string, Perl allocates about 20Mb memory.

    undefing the variable makes Perl allocate about 10Mb less:

    #!/usr/bin/perl use strict; use warnings; use Readonly; Readonly my $size => 10 * 1024 ** 2; # 10 Mb. sub show_mem { system "grep ^VmSize /proc/$$/status"; } sub gimme_big { my $size = shift; my $var = 'x' x $size; undef $var; } show_mem; gimme_big $size; show_mem; __END__ VmSize: 3472 kB VmSize: 13716 kB
    So, how do we get rid of the extra 10Mb? By a careful use of string eval:
    #!/usr/bin/perl use strict; use warnings; use Readonly; Readonly my $size => 10 * 1024 ** 2; # 10 Mb. sub show_mem { system "grep ^VmSize /proc/$$/status"; } sub gimme_big { my $size = shift; my $var = eval "'x' x $size"; undef $var; } show_mem; gimme_big $size; show_mem; __END__ VmSize: 3468 kB VmSize: 3468 kB

Log In?
Username:
Password:

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

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

    No recent polls found