Perl memory usage profiling and leak detection?

前端 未结 5 1933
名媛妹妹
名媛妹妹 2020-11-27 14:58

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

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

5条回答
  •  温柔的废话
    2020-11-27 15:19

    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\n";
                    # 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
    }
    

提交回复
热议问题