I have a long-running Web app running under Perl 5.16.0 that processes hundreds of requests per minute and sadly leaks RAM ... very slowly, maybe 10-20 MB per day. I was able to figure out more major memory leaks in the past using the following code (which is set to run once per minute in the main event loop):
if (!%first_arena) { %first_arena = %{arena_ref_counts()}; my $all = Devel::Gladiator::walk_arena(); for my $it (@$all) { if (ref $it eq 'ARRAY') { $arrays{refaddr($it)} = time(); } } $all = undef; } else { my %current_arena = %{arena_ref_counts()}; my %increase; for my $type (keys %current_arena) { if (!exists $first_arena{$type}) { $increase{$type} = $current_arena{$type}; } elsif ($current_arena{$type} - $first_arena{$type} > 2) { $increase{$type} = $current_arena{$type} - $first_arena{$t +ype}; } } if (%increase) { print "\nincreases\n"; for my $type (sort { $increase{$b} <=> $increase{$a} } keys %i +ncrease) { print "$increase{$type}\t$type\n"; } print '%client: '.(scalar keys %client)."\n"; print '%comet: '.(scalar keys %comet)."\n"; print '@ready: '.(scalar @ready)."\n"; print '%async: '.(scalar keys %async)."\n"; my %leaked_arrays; my $all = Devel::Gladiator::walk_arena(); for my $it (@$all) { if (ref $it eq 'ARRAY' and !exists $arrays{refaddr($it)} a +nd ++$array_count{refaddr($it)}{md5_hex(Dumper $it)} >= 3) { $leaked_arrays{refaddr($it)} = $it; } } $all = undef; if (%leaked_arrays) { my $zero_length = scalar grep { scalar @{$leaked_arrays{$_ +}} == 0 } keys %leaked_arrays; my $non_zero_length = scalar grep { scalar @{$leaked_array +s{$_}} > 0 } keys %leaked_arrays; print "leaked arrays: $zero_length zero length, $non_zero_ +length non zero length\n"; } } }
After a half hour or so, this prints:
increases 24202 SCALAR 7482 REF 5966 HASH 5911 REF-HASH 4387 ARRAY 592 REF-SCALAR 552 REF-DBI::st 368 REF-DBI::db 368 DBI::st 184 DBD::Pg::st_mem 155 GLOB 74 CODE 57 REF-IO::Socket::INET 30 IO::File 29 IO::Socket::INET 22 REGEXP %client: 28 %comet: 28 @ready: 0 %async: 0 leaked arrays: 3830 zero length, 156 non zero length
The only real leaks are the ARRAYs. Growth of non zero length ARRAYs is almost flat, but zero length ones grow at an average rate of 120 per minute! Why does it think it needs so many zero length ARRAYs?!

Does anyone know what kind of practices could cause this bizarre behavior? Or maybe some other tools I could use to find the source of the leak? A way to tell the line on which the zero-length ARRAYs were allocated would be perfect. I know about Devel::LeakTrace::Fast, but it requires a perl built for debugging, doesn't it? I can do that if I need to, but ...

Thank you, monks!


In reply to Leaking 0-length ARRAYs!? by njahnke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.