in reply to Re^2: out of memory problem
in thread out of memory problem
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:
sub foo{ my $var = 'xyz' x 1_000_000; } sub bar{ my $var2 = 'xyz' x 1_000_000; } warn "ONE\n"; foo(); warn "TWO\n"; foo(); warn "THREE\n"; bar(); warn "FOUR\n";
Running this through strace gives this output:
[...] write(2, "ONE\n", 4) = 4 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb7956000 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb7679000 write(2, "TWO\n", 4) = 4 write(2, "THREE\n", 6) = 6 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb739c000 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb70bf000 write(2, "FOUR\n", 5) = 5
This shows that perl does not allocate memory between "TWO" and "THREE" (the variable in sub foo gets reused), but it does allocate new memory before running bar, even though it could reuse $var's memory for $var2.
Including system('ps', '-Orss', '-p', $$) and die "$! $?"; in various places leads to the same conclusion without strace.
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: out of memory problem
by ikegami (Patriarch) on Sep 13, 2008 at 15:56 UTC | |
by betterworld (Curate) on Sep 13, 2008 at 20:44 UTC | |
by ikegami (Patriarch) on Sep 13, 2008 at 21:26 UTC | |
by BrowserUk (Patriarch) on Sep 13, 2008 at 21:37 UTC |