Hi All,

I've recently written some parser code and today discovered what appears to be a memory leak somewhere in the code. Debugging and stripping down the code, I've managed to construct a toy example that illustrates the problem. Here's the code:

#!/usr/bin/perl use strict; use warnings; open(my $fh, '<', 'EXAMPLE.TXT'); my $regexp = qr/(?<value1>\d+)\s+(?<value2>\d+)/; while(<$fh>) { next unless /$regexp/; my $value1 = $+{value1}; my $value2 = $+{value2}; print "GOT $value1 $value2\n"; }

The code is simply using 2 named capture buffers in a regexp to parse out numeric values.

'EXAMPLE.TXT' is just a text file consisting of a pair of numbers on each line. I used

1 2 3 4 5 6 7 8 ...

And so on, for about 100,000 lines, though it doesn't really have to be that long.

I'm working in ActiveState perl, v5.10.0, WinXP x86, and using the task manager to observe how much memory perl uses as it parses the file. Usage steadily increases until the script finishes. For this toy example, it's not so much of an issue, but in my actual project it gets out of hand rather fast.

I've noticed that switching over to $1 and $2 rather than $+{value1} and $+{value2} eliminates the problem, but I prefer using the named capture buffers for clarity as things get big & hairy.

My question is...why? I was assuming that the my-scoped variables within the loop would go out of scope each iteration and free up any references to %+'s elements. I'm aware that %+ is a tied hash, but am not familiar enough with the details of tied hashes to figure out what's going wrong.

Thanks
-Maph


In reply to Memory Leaks and %+ by MaphsterB

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.