Perl memory usage profiling and leak detection?

前端 未结 5 1945
名媛妹妹
名媛妹妹 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 15:19

    You can use Devel::Leak to search for memory leaks. However, the documentation is pretty sparse... for example, just where does one get the $handle reference to pass to Devel::Leak::NoteSV()? f I find the answer, I will edit this response.

    Ok it turns out that using this module is pretty straightforward (code stolen shamelessly from Apache::Leak):

    use Devel::Leak;
    
    my $handle; # apparently this doesn't need to be anything at all
    my $leaveCount = 0;
    my $enterCount = Devel::Leak::NoteSV($handle);
    print STDERR "ENTER: $enterCount SVs\n";
    
    #  ... code that may leak
    
    $leaveCount = Devel::Leak::CheckSV($handle);
    print STDERR "\nLEAVE: $leaveCount SVs\n";
    

    I'd place as much code as possible in the middle section, with the leaveCount check as close to the end of execution (if you have one) as possible -- after most variables have been deallocated as possible (if you can't get a variable out of scope, you can assign undef to it to free whatever it was pointing to).

提交回复
热议问题