Re: Memory allocation in 5.6
by Yitzchak Scott-Thoennes other posts by this author
Sep 29 2000 10:11AM messages near this date
Memory allocation in 5.6
|
how does 'for my $x (LIST)' alias?
In article <v0422080cb5f99c3355cf@[192.168.0.4]> ,
"Marc D. Spencer" <marcs@[...].com> wrote:
> my $foo = 'x' x ($ARGV[0] * 1024000);
> sleep $ARGV[1] if $ARGV[1];
> print "Released.\n";
>
> seems to allocate twice the memory I ask it for. This wouldn't be a
> problem, except I'm manipulating images in scalars, so they can be
> large.
The x operator has pad space allocated to it to store its result in.
(The same is true of most other operators.) This is not freed and
gets reused each time the code is run through.
You could try:
my $foo = 'x';
$foo x= ($ARGV[0] * 1024000);
If that also doubles the space, try:
sub repeater($$) { my ($str, $count) = @_; sub { $str x $count } }
my $foo = &{&repeater('x', $ARGV[0] * 1024000)}
This should free the extra memory right away (to your program, not
back to the system). But I'm not sure that it won't allocate triple
the memory to your program.
Thread:
Marc D. Spencer
Yitzchak Scott-Thoennes
|