本文介绍了垃圾收集在Perl中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Java不同,Perl使用引用计数来进行垃圾回收。我曾尝试搜索一些前面提到的有关C ++ RAII和智能指针和Java GC的问题,但尚未理解Perl如何处理循环引用问题。

任何人都可以解释Perl的垃圾收集器如何处理循环引用吗?有没有什么方法可以回收程序不再使用的循环引用内存,或者Perl完全忽略了这个问题?

解决方案

根据我的 Programming Perl 3rd ed。的副本,在退出Perl 5时,会执行昂贵的标记和清除来回收循环引用。您希望尽可能避免循环引用,否则在程序退出前它们不会被回收。



Perl 5确实通过模块。

Perl 6将会转移到一个可插拔的垃圾收集方案(呃,,这些选项的行为可能会对Perl产生影响)。也就是说,你可以选择各种垃圾收集器,或者实现你自己的垃圾收集器。想要一个复制收藏家?当然。想要着色收藏家?你说对了。标记/扫描,压缩等?为什么不呢?


Unlike Java, Perl uses reference count for garbage collection. I have tried searching some previous questions which speak about C++ RAII and smart pointers and Java GC but have not understood how Perl deals with the circular referencing problem.

Can anyone explain how Perl's garbage collector deals with circular references? Is there any way to reclaim circular referenced memory which are no longer used by the program or does Perl just ignores this problem altogether?

解决方案

According to my copy of Programming Perl 3rd ed., on exit Perl 5 does an "expensive mark and sweep" to reclaim circular references. You'll want to avoid circular references as much as possible because otherwise they won't be reclaimed until the program exits.

Perl 5 does offer weak references through the Scalar::Utils module.

Perl 6 will move to a pluggable garbage collected scheme (well, the underlying VM will have multiple garbage collection options and the behavior of those options can have an effect on Perl). That is, you'll be able to choose between various garbage collectors, or implement your own. Want a copying collector? Sure. Want a coloring collector? You got it. Mark/sweep, compacting, etc? Why not?

这篇关于垃圾收集在Perl中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:54