当释放大型数组时,为什么Perl不会垃圾回收内存? [英] Why does Perl not garbage collect memory when a large array is deallocated?

查看:73
本文介绍了当释放大型数组时,为什么Perl不会垃圾回收内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Perl使用基于引用计数的垃圾收集. 当变量超出范围时,引用计数将减少,并且如果REFcount变为0,则会取消分配内存. 但是,当我跟踪下面显示的一个小示例时,我无法找到正在发生的取消分配情况.

I know that Perl uses reference count based garbage collection. When a variable goes out of scope, the reference count is decremented and if REFcount goes to 0, the memory is de-allocated. But when I trace a small example which is shown below, I couldn't able to find the de-allocation happening.

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()调用之后,应该对已分配的内存进行垃圾回收.为什么不做呢?

In the example, when the program starts, Perl.exe occupies ~ 3 MB physical memory. After allocation during the func() call, Perl.exe occupies ~ 370 MB memory. But after the func() call , the allocated memory should be garbage collected. why is it not done?

期待您的答复.

推荐答案

根据问题"

通常不能.分配给词汇的内存(即my()变量) 即使它们超出范围,也无法回收或重用.它是 保留,以防变量返回范围.内存分配 全局变量可以通过使用 undef()和/或delete().

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().

在大多数操作系统上,分配给程序的内存永远不能 返回系统.这就是为什么长时间运行的程序有时会重新 自己执行.某些操作系统(尤其是使用 mmap(2)用于分配大块内存)可以回收 不再使用,但是在这样的系统上,必须配置perl并 编译以使用操作系统的malloc,而不是perl.

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.

通常,内存分配和取消分配与您无关 在Perl中可能会或应该担心很多.

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

另请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆