问题描述
我的脚本存在内存泄漏.我相信是因为在对嵌套对象执行undef
之后,脚本中的内存量保持不变.我已经使用 Devel :: Cycle 来找到任何循环引用,而我已使用Scalar::Util
将这些循环引用转换为弱引用.问题仍然存在.
I have a script that has memory leaks. I believe this is so because after I perform undef
on my nested objects, the amount of memory in script is unchanged. I have used Devel::Cycle to locate any cyclical references and I have turned those cyclical references into weak references with Scalar::Util
. The problem still remains.
现在,我正在尝试使用Valgrind解决此问题.首先从valgrind开始,我在perl hello world程序中测试了这些东西:
Now I am trying to use Valgrind to solve the issue. As a first start with valgrind, I tested things out a perl hello world program:
#! /usr/bin/perl
use strict;
use warnings;
print "Hello world!\n";
这是运行valgrind --trace-children=yes perl ./hello_world.pl
时的valgrind输出:
Here was the valgrind output when running valgrind --trace-children=yes perl ./hello_world.pl
:
==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
的数量,则发生内存泄漏.
My understanding, from here, is that when the number of allocs
does not equal the number of frees
you have a memory leak.
由于我正在做的是打印hello world,因此我不得不问这个问题,Perl解释器本身(这里是v5.10.1)是否至少有其自身的内存泄漏,或者我将所有事情解释都错了吗?
Since all I'm doing is printing hello world, I'm forced to ask the question, does the Perl interpreter itself, here v5.10.1, have at least its own memory leak or am I interpreting things all wrong?
在处理实际的perl脚本之前,我想了解这一点.
I would like to understand this before I tackle my actual perl script.
添加
我在 Perl 5.12.0 delta 中看到以下内容:
I see in Perl 5.12.0 delta, the following:
这最终可能会适用于我完整的perl脚本,而不是此hello world程序,但这使我认为我应该经历将非Perl最新版本安装为非root的痛苦.
This may ultimately apply to my complete perl script, and not this hello world program, but it's leading me to think that I should go through the pain of installing the latest version of perl as non-root.
ADDENDUM2
我安装了activestate perl 5.16.3,问题仍然存在,还有我的实际脚本问题仍然存在.
I installed activestate perl 5.16.3, and the problem, and also my actual script's problems, still remains.
我怀疑在这个hello world程序的情况下,我必须使用/解释valgrind的方式不正确,但是我不知道在哪里.
I suspect that in the case of this hello world program, I must be using/interpreting valgrind improperly but I don't understand where yet.
UPDATE1 达西姆的回答确实有所作为.当我在perl脚本中引入以下行时:
UPDATE1Daxim's answer does make a difference. When I introduce the following line in my perl script:
use Perl::Destruct::Level level => 1;
然后valgrind输出为:
Then the valgrind output is:
==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)
有很大的不同.我自己的脚本仍然存在内存泄漏问题,但至少现在这个瓦尔世界程序对valgrind似乎很明智.
Which is a substantial difference. My own memory leak problems with my script remain but at least this hello world program now seems sensible to valgrind.
这整件事虽然引出了一个问题,如果在程序退出之前没有释放任何内存,那么用Scalar::Util
停止硬循环引用的意义何在,而禁止使用这个有点深奥的Perl::Destruct::Level
模块??
This whole thing though begs the question, what is the point of stopping hard cyclical references with Scalar::Util
if no memory is freed until the program exits, baring the use of this somewhat esoteric Perl::Destruct::Level
module???
推荐答案
泄漏是故意的.在#p5p中文森特注释:
The leak is intentional. vincent in #p5p comments:
这篇关于Perl v5.10.1是否存在内存泄漏或如何解释valgrind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!