Why does my Perl regex use so much memory?

I run a regex against a large scalar. Although this match does not capture anything, my process is growing by 30 million after this match:

# A
if (${$c} =~ m/\G<<\s*/cgs)
{
    #B
    ...
}

$c- a link to a rather large scalar (about 21 M), but I confirmed that I pos(${$c})was in the right place, and the expression matches the first character, while pos(${$c})updating to the right place after the match. But, as I mentioned, the process grew by about 30M between #A and #B, although I am not collecting anything in this match. Where does my memory go?

Edit: Yes, use $&was to blame. We use Perl 5.8.8, and my script used Getopt :: Declare , which uses the built-in Text :: Balanced . The 1.95 version of this module used $&. Version 2.0.0, which ships with Perl 5.10, removed the link to $&and fixes the problem.

+5
source share
1 answer

Just quickly check if you mention $ &, $ `or $ '(sometimes called $ MATCH, $ PREMATCH and $ POSTMATCH) anywhere in your code? If so, Perl will copy your entire string for each regular expression, just in case you want to check these variables.

" " , , , use English, use English qw( -no_match_vars ).

, Devel::SawAmpersand, , , Devel::FindAmpersand, , .

( Perl ?), , , , , , , .

Cheerio,

+20

All Articles