2009年5月14日星期四

[PerlChina] Re: 如何主动回收对象的内存空间

http://is.gd/xi6v <----- came across this from perlbuzz :-)

2009/5/13 cnhack TNT <cnhacktnt@gmail.com>
By the way, Devel::Cycle is a great module to find memory cycles in objects.
:-)


2009/5/13 agentzh <agentzh@gmail.com>
2009/5/13 cnhack TNT <cnhacktnt@gmail.com>

类似这样的循环引用,确实 undef 会不靠谱, 但是 perl 本身的 two-phased garbage collection 机制仍然会保证 new 出来的对象在最后时刻被  DESTORY 掉, 也就是在 perl 的 global destruction 过程中被析构。

Alias++ 的 Object::Destroyer 在这里也许会有所帮助,哈哈,当然不是正解了,哈哈。。。循环引用用 weaken 搞定之。。。Devel::Leak::Object 在这里可以作为验证(当然不能保证泄漏在其他地点发生。。。)
 
我有个问题问 agent, 如何方便的知道,某个变量/值当前的引用计数是多少?
 特别方便的不太清楚,呵呵。一般我用 Devel::Peek 模块:

$ perl -e 'use Devel::Peek; $a = \$a; Dump($a)'
SV = RV(0x81812f8) at 0x815469c
  REFCNT = 2
  FLAGS = (ROK)
  RV = 0x815469c
  SV = RV(0x81812f8) at 0x815469c
    REFCNT = 2
    FLAGS = (ROK)
    RV = 0x815469c
    SV = RV(0x81812f8) at 0x815469c
      REFCNT = 2
      FLAGS = (ROK)
      RV = 0x815469c
      SV = RV(0x81812f8) at 0x815469c
        REFCNT = 2
        FLAGS = (ROK)
        RV = 0x815469c
        SV = RV(0x81812f8) at 0x815469c
          REFCNT = 2
          FLAGS = (ROK)
          RV = 0x815469c

注意这里的 REFCNT 的值,呵呵。。。

记得在 Firefox 内部,JavaScript 原生对象是 GC 方式,而 XPCOM 组件却也是引用计数方式,两种东西混合在一起时比较容易泄漏。。。也难调了。。。呵呵。。。题外话了。。。

Cheers,
-agentzh





--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
 要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
 要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛

-~----------~----~----~----~------~----~------~--~---

没有评论: