Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: out of memory problem

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


in reply to out of memory problem

@data=split (/\s+/,$_); $a=$data[0]; $b=$data[3]; $c=$data[4]; $d=$data[5];

Note that because you don't use "my" here, all these variables will stay in memory even after the loop has finished. If the last line is very long, this might be a memory problem.

Anyway, this code does not look like it is strict. You should fix this, and maybe then you discover more problems in your code.

Replies are listed 'Best First'.
Re^2: out of memory problem
by ikegami (Patriarch) on Sep 13, 2008 at 13:00 UTC

    Note that because you don't use "my" here, all these variables will stay in memory even after the loop has finished.

    That happens when using my too. Neither the SV nor the string buffer are freed. They are simply cleared.

    Update: The following isn't proof, but it does let you see the effect of what I described.

      You're right. The reuse of the address could be coincidence, but as the LEN stay 12, we can conclude that the buffer was never freed.

      I've done a little bit of testing too:

      One question remains: When does perl free memory? I can't quite believe that every lexical variable that has ever been used results in large stale memory blocks. This would be against the spirit of this excerpt from perlsyn:

      You wouldn't want memory being free until you were done using it, or kept around once you were done. Automatic garbage collection takes care of this for you.

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found