perl -E"$x=chr(0); $x x= 100e6; <>; $r = \substr $x, 50e6; <>"
Watch the process memory with ProcessManager:
- When it pauses the first time memory usage is ~98MB. Hit enter
- Now the memory usage shoots up to ~145MB.
All that's changes is that it has taken a substr ref to the second half of the string.
I suspect it is a result of this "fix".
But I believe that fix is wrong, and that the original bug is spurious. (At least in part.)
If you do this: my $ref;
{
my $string = "123";
$ref = \$string;
}
## Here $string persists.
No one is surprised that (the memory allocated to) $string persists beyond the block.
Why should this be any different: my $ref;
{
my $string = "123";
$ref = \substr $string, 1, 1;
}
## This was the (IMO false) "bug" scenario.
In this case -- when the string goes out of scope -- I could make arguments for 3 possible outcomes:
- The memory allocated to $string persists:
$ref continues to refer (directly) to the memory allocate to $string.
Those parts of $string outside of $ref are inaccessible and irrecoverable until $ref is GC's.
- The memory allocated to $string is GC'd; $ref becomes undef.
Probably difficult to orchestrate.
- The memory allocated to $string persists until $ref is used the next time:
At which point the extraneous (pre and/or post fix) bits of $string are GC'd leaving $ref pointing at just that which it references.
This would require retaining a pointer back to 'parent' string with the lvalue ref, and when the lvalue magic fires it checks the refcount of its parent.
If that refcount is 1 -- meaning it holds the only reference to it, it frees off the extraneous parts of the parent.
Of the three, I'd prefer C, but would find A completely in keeping with Perl's philosophy.
Duplicating the referenced memory of every lvalue ref and the having to copy it back each time it is modified -- just to cover off a really obscure scenario that is at worst, only mildly anomalous -- is crazy.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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".
|