Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: out of memory problem

by betterworld (Curate)
on Sep 13, 2008 at 20:44 UTC ( [id://711166]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2026-01-17 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (121 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.