in reply to Re^3: out of memory problem
in thread out of memory problem

but it does allocate new memory before running bar, even though it could reuse $var's memory for $var2.

No, that would defy the optimization of not deallocating it in the first place.

For Perl to know when $var is not currently being used, it would have to 1) place $var on a free list when it stops being used and 2) remove it from the free list when $var is in use again. Those two operations are known as deallocation and allocation, exactly what the optimization is trying to avoid!

Keep in mind that each variable consists of two memory blocks, three if they have a string buffer associated with them. And that's just for non-magical scalars. A lot of CPU time is being saved.

One question remains: When does perl free memory?

Anon SVs are freed. SVs that become anon (such as return values) are freed. SVs passed to undef have their attached buffer freed.

Keep in mind that all of this is the result of an (undocumented?) optimization. It shouldn't be relied upon. It's just that you can't rely on it not happening.

Replies are listed 'Best First'.
Re^5: out of memory problem
by betterworld (Curate) on Sep 13, 2008 at 20:44 UTC

    Suppose I write a module which provides a "frobnicate" function:

    sub frobnicate { my ($string) = @_; $string =~ s/foo/bar/g; return $string; }

    Suppose I write a program that runs for a really long time, and uses "frobnicate" only once during its initialization, and it passes a really long string to it.

    Should I expect that $string will hold this long string for the program's lifetime? Am I supposed to undef every single variable to avoid this? (Which would pretty much nullify this "optimization", but it seems to be the only way to avoid the memory leak.)

    Update: Replaced the code of the example function.

      Yes, as far as I know

      The way to avoid that problem when you know you are dealing with large scalars, (arrays and hashes too), is to use pass by reference. Either:

      sub frobnicate { $_[0] =~ s/foo/bar/g; return; }

      Or:

      sub frobnicate { my ($ref) = @_; $$ref =~ s/foo/bar/g; return; }

      Either way you avoid a lot of copying and the lingering redundant memory usage.


      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.