Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Strange memory growth?

by welchavw (Pilgrim)
on Nov 07, 2003 at 21:33 UTC ( #305455=note: print w/ replies, xml ) Need Help??


in reply to Strange memory growth?

http://nntp.x.perl.org/group/perl.perl5.porters/51374 is interesting and relevant, I think.

,welchavw


Comment on Re: Strange memory growth?
Re: Re: Strange memory growth?
by hardburn (Abbot) on Nov 07, 2003 at 22:01 UTC

    Ahh, now I see. There is the possibility that the if block is inside a loop, so perl will keep the lexical around so we don't have to recreate it on the next run through the loop.

    So if this is really a problem, you could always do:

    while($condition) { # do something $condition = 0; # Make sure condition is false at the end }

    Please, only if you've anaylized the program and you really are in danger of running out of memory (including swap space) from the conditionals you're doing. Anything else is a micro-optimization.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Are you sure?... see my second post above. Even when I use the *same* *package* var, there is still unexplained memory growth. Your point about keeping the lexical around to avoid re-creating it next time through the loop doesn't seem applicable. In that case, I'm not using a lexical, nor am I creating a variable more than once.

      Update: Here's an even simpler example:

      $foo = 'perl' x 1000000; sleep 5; $foo = 'perl' x 1000000; sleep 5; $foo = 'perl' x 1000000; sleep 5; $foo = 'perl' x 1000000; sleep 5; $foo = 'perl' x 1000000; sleep 5; $foo = 'perl' x 1000000; sleep 5;

      Here I'm assigning the same value to the same variable over and over, why is memory use increasing? It seems too simple to be a bug, but still seems hard to explain.

        Most perl operators that return a value are allocated a private SV to hold their result. XS writers would know this as the TARG. In the case that five 'x' operators appear in the source, five SVs would be created. Each time that particular 'x' is executed, the corresponding SV has a million-byte string assigned to it. Perl does it this was because it's fast. If you put the whole thing in a loop and run it 100 times, it doesn't doesn't take up any more memory.

        The other efrect is that at the end of scope, the underlying memory structures that implement lexical vars are not usually destroyed; instead they hang around on the assumption that next time round the loop a new lexical variable of similar type will have to be created. So it's a speed/memory tradeoff.

      Please, only if you've anaylized the program and you really are in danger of running out of memory (including swap space) from the conditionals you're doing. Anything else is a micro-optimization.

      Possibly. But please remember when Perl is used in a mod_perl environment with a prefork MPM, you want to keep as much memory shared between children. This behaviour of Perl is very counter-productive in that environment: I'd rather take a little CPU hit for the overhead of creating the lexical again, and not unsharing any further memory, than have the current situation.

      More generally, I think the current behaviour of Perl to use more memory rather than more CPU, becomes more and more counter-productive as the speed of accessing memory is growing slower than the speed of the CPU with each new generation of systems.

      Liz

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-07-24 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls