Perl 内存使用分析和泄漏检测? [英] Perl memory usage profiling and leak detection?

查看:47
本文介绍了Perl 内存使用分析和泄漏检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Perl 编写了一个在 Linux 上运行的持久网络服务.

I wrote a persistent network service in Perl that runs on Linux.

不幸的是,当它运行时,它的常驻堆栈大小 (RSS) 只会缓慢而稳定地增长、增长、再增长.

Unfortunately, as it runs, its Resident Stack Size (RSS) just grows, and grows, and grows, slowly but surely.

尽管我努力删除所有不需要的哈希键并删除所有对对象的引用,否则这些引用会导致引用计数保持原位并阻碍垃圾收集.

This is despite diligent efforts on my part to expunge all unneeded hash keys and delete all references to objects that would otherwise cause reference counts to remain in place and obstruct garbage collection.

是否有任何好的工具可以在 Perl 程序中分析与各种本机数据原语、受祝福的哈希引用对象等相关的内存使用情况?你用什么来追踪内存泄漏?

Are there any good tools for profiling the memory usage associated with various native data primitives, blessed hash reference objects, etc. within a Perl program? What do you use for tracking down memory leaks?

我不习惯在 Perl 调试器或任何各种交互式分析器中花费时间,因此将感谢您的热情、温和、非深奥的回应.:-)

I do not habitually spend time in the Perl debugger or any of the various interactive profilers, so a warm, gentle, non-esoteric response would be appreciated. :-)

推荐答案

您可以在其中一个对象中使用循环引用.当垃圾收集器来释放这个对象时,循环引用意味着该引用所引用的所有内容将永远不会被释放.您可以使用 Devel::CycleTest::Memory::Cycle.要尝试的一件事(尽管它在生产代码中可能会变得昂贵,因此在未设置调试标志时我会禁用它)是检查所有对象的析构函数内的循环引用:

You could have a circular reference in one of your objects. When the garbage collector comes along to deallocate this object, the circular reference means that everything referred to by that reference will never get freed. You can check for circular references with Devel::Cycle and Test::Memory::Cycle. One thing to try (although it might get expensive in production code, so I'd disable it when a debug flag is not set) is checking for circular references inside the destructor for all your objects:

# make this be the parent class for all objects you want to check;
# or alternatively, stuff this into the UNIVERSAL class's destructor
package My::Parent;
use strict;
use warnings;
use Devel::Cycle;   # exports find_cycle() by default

sub DESTROY
{
    my $this = shift;

    # callback will be called for every cycle found
    find_cycle($this, sub {
            my $path = shift;
            foreach (@$path)
            {
                my ($type,$index,$ref,$value) = @$_;
                print STDERR "Circular reference found while destroying object of type " .
                    ref($this) . "! reftype: $type
";
                # print other diagnostics if needed; see docs for find_cycle()
            }
        });

    # perhaps add code to weaken any circular references found,
    # so that destructor can Do The Right Thing
}

这篇关于Perl 内存使用分析和泄漏检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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