我有一个存在内存泄漏的脚本。我认为出现这种情况是因为在对嵌套对象执行
从这里了解到,当
由于我只是打印hello world,我不得不问一个问题,Perl解释器本身在这里(v5.10.1)是否至少有自己的内存泄漏,或者我完全理解错了?
在着手处理我的实际perl脚本之前,我想弄清楚这个问题。 补充说明 我在Perl 5.12.0 delta中看到以下内容:
我怀疑在这个hello world程序的情况下,我必须错误地使用/解释valgrind,但我还没有弄清楚具体在哪里。 更新1 Daxim的答案确实有所不同。当我在perl脚本中添加以下行时:
undef
后,脚本中的内存量没有改变。我使用了 Devel::Cycle 来定位任何循环引用,并将这些循环引用转换为弱引用,使用了 Scalar::Util
。问题仍然存在。
现在我正在尝试使用 Valgrind 来解决这个问题。作为开始使用 valgrind 的第一步,我测试了一下 Perl 的 hello world 程序:
#! /usr/bin/perl
use strict;
use warnings;
print "Hello world!\n";
运行 valgrind --trace-children=yes perl ./hello_world.pl
时,以下是 valgrind 输出的内容:
==12823== HEAP SUMMARY:
==12823== in use at exit: 290,774 bytes in 2,372 blocks
==12823== total heap usage: 5,159 allocs, 2,787 frees, 478,873 bytes allocated
==12823==
==12823== LEAK SUMMARY:
==12823== definitely lost: 13,981 bytes in 18 blocks
==12823== indirectly lost: 276,793 bytes in 2,354 blocks
==12823== possibly lost: 0 bytes in 0 blocks
==12823== still reachable: 0 bytes in 0 blocks
==12823== suppressed: 0 bytes in 0 blocks
==12823== Rerun with --leak-check=full to see details of leaked memory
==12823==
==12823== For counts of detected and suppressed errors, rerun with: -v
==12823== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
从这里了解到,当
allocs
的数量不等于frees
的数量时会出现内存泄漏。由于我只是打印hello world,我不得不问一个问题,Perl解释器本身在这里(v5.10.1)是否至少有自己的内存泄漏,或者我完全理解错了?
在着手处理我的实际perl脚本之前,我想弄清楚这个问题。 补充说明 我在Perl 5.12.0 delta中看到以下内容:
这可能最终适用于我完整的perl脚本,而不是这个hello world程序,但这使我认为我应该通过非root方式安装最新版本的perl。 补充说明2 我已经安装了ActiveState Perl 5.16.3,但问题仍然存在,也存在于我的实际脚本中。对哈希表的弱引用将泄漏。这影响了DBI [RT #56908]。
我怀疑在这个hello world程序的情况下,我必须错误地使用/解释valgrind,但我还没有弄清楚具体在哪里。 更新1 Daxim的答案确实有所不同。当我在perl脚本中添加以下行时:
use Perl::Destruct::Level level => 1;
然后是valgrind的输出:
==29719== HEAP SUMMARY:
==29719== in use at exit: 1,617 bytes in 6 blocks
==29719== total heap usage: 6,499 allocs, 6,493 frees, 585,389 bytes allocated
==29719==
==29719== LEAK SUMMARY:
==29719== definitely lost: 0 bytes in 0 blocks
==29719== indirectly lost: 0 bytes in 0 blocks
==29719== possibly lost: 0 bytes in 0 blocks
==29719== still reachable: 1,617 bytes in 6 blocks
==29719== suppressed: 0 bytes in 0 blocks
==29719== Rerun with --leak-check=full to see details of leaked memory
==29719==
==29719== For counts of detected and suppressed errors, rerun with: -v
==29719== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
这是一个很大的差别。我的脚本自己的内存泄漏问题仍然存在,但至少这个hello world程序现在对valgrind来说似乎是合理的。
然而,整个问题是,如果没有这个有点难懂的Perl::Destruct::Level
模块的使用,除了在程序退出时没有释放内存外,停止硬循环引用的Scalar::Util
有什么意义???