in reply to Re: Re: Strange memory growth?
in thread Strange memory growth?

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Strange memory growth?
by Anonymous Monk on Nov 09, 2003 at 18:05 UTC
    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.