我一直在研究工作中的perl项目,并遇到了一个奇怪的内存泄漏.我把我的问题的根源归结为一个人为的例子:
#!/usr/bin/perl
use strict;
use warnings;
# takes: an array reference
# returns: 1
sub g {
my ($a) = @_;
return 1;
}
# takes: nothing
# returns: the result of applying g on an array reference
sub f {
my @a = ('a') x 131072; # allocate roughly a megabyte
return g(\@a);
}
# causes a leak:
#map { f($_) } (1..100000);
# loop equivalent to map, no leak:
#my @b;
#for my $i (1..100000) {
# push …Run Code Online (Sandbox Code Playgroud)