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

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.

Replies are listed 'Best First'.
Re^6: out of memory problem
by ikegami (Patriarch) on Sep 13, 2008 at 21:26 UTC
    Yes, as far as I know
Re^6: out of memory problem
by BrowserUk (Patriarch) on Sep 13, 2008 at 21:37 UTC

    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.