小编Jam*_*son的帖子

Perl map/grep内存泄漏

我一直在研究工作中的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)

perl grep memory-leaks map

6
推荐指数
1
解决办法
600
查看次数

标签 统计

grep ×1

map ×1

memory-leaks ×1

perl ×1