为什么在取消分配大数组时,Perl不会垃圾收集内存?

我知道Perl使用基于引用计数的垃圾收集.
当变量超出范围时,引用计数递减,如果REFcount变为0,则内存被取消分配.
但是,当我追踪下面显示的一个小例子时,我无法找到解除分配的情况.
print "start..";
sub func
{
    my $length = 8*1024*1024;
    my $array = [1..$length];
}

func();

print "done..";

在该示例中,当程序启动时,Perl.exe占用大约3 MB的物理内存.
在func()调用中分配后,Perl.exe占用了大约370 MB的内存.
但是在func()调用之后,分配的内存应该被垃圾回收.为什么不做?

期待您的回复.

解决方法

根据 perlfaq3年的“ How can I free an array or hash so my program shrinks?”问题:

You usually can’t. Memory allocated to lexicals (i.e. my() variables)
cannot be reclaimed or reused even if they go out of scope. It is
reserved in case the variables come back into scope. Memory allocated
to global variables can be reused (within your program) by using
undef() and/or delete().

On most operating systems,memory allocated to a program can never be
returned to the system. That’s why long-running programs sometimes re-
exec themselves. Some operating systems (notably,systems that use
mmap(2) for allocating large chunks of memory) can reclaim memory that
is no longer used,but on such systems,perl must be configured and
compiled to use the OS’s malloc,not perl’s.

In general,memory allocation and de-allocation isn’t something you
can or should be worrying about much in Perl.

See also 07002

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...