mnperez has asked for the wisdom of the Perl Monks concerning the following question:

Someone sent this to a mailing list I am on, its a small perl program that when you execute it with the proper option (aptly named 'death' :), it will just suck up a ton of memory, similiar to a malloc() fork bomb. Anyone know why?
#!/usr/bin/perl # To burn up memory quickly, run this with option "death". $option = shift; if ($option eq "death") { $line = "1::"; } else { $line = "1:"; } while (1) { @X = split(/:/, $line); }

Replies are listed 'Best First'.
Re: Perl memory leak?
by perlplexer (Hermit) on Apr 17, 2002 at 18:32 UTC
    Scary...
    perl -e "while (1){@X=split/:/,'1::'}"
    Slurps 170MB of RAM in about 3 seconds on my NT box...
    I'm not that good with Perl internals; however, the default behavior of split() is to ignore trailing empty fields. I'm thinking Perl takes the "ignoring" to the next level and "forgets" to release the memory; If you do
    perl -e "while (1){@X=split/:/,'1::',-1}"
    then everything works as expected. "-1" tells split() to treat trailing empty fields as valid; i.e., not to ignore them.

    --perlplexer
      Hmm... or maybe not... :)
      my()ing the @X seems to fix it as well.
      perl -e "while (1){my @X=split/:/,'1::'}"
      (use strict;)++

      --perlplexer

      Update:added "my"
        my()ing @X outside the while works as well...

        perl -e'my@X;while(){@X=split/:/,"1::"}'
        good catch, perlplexer.

        ~Particle ;Þ

      Try

      perl -MO=Deparse -e "while (1){@X=split/:/,'1::',-1}"

      Crashes perl on my W2K and my NT sp6 boxes! Windows tells me an error log is being created, but no log is created.

      Update:

      Wed 04/17/2002 18:43:53.45 C:\>perl -MO=Deparse -e "@X=split /:/,'1::'"

      Take away the loop, still crashes.
      Wed 04/17/2002 18:43:43.28 C:\>perl -MO=Deparse -e "@X=split /p/,'1pp'"

      s/:/p/, still crashes. Take away the @X, works fine.
      Has a bug report been submitted?
Re: Perl memory leak?
by Chmrr (Vicar) on Apr 18, 2002 at 01:42 UTC

    I have managed to reduce this to:

    perl -e 'split ?a?, "a" while 1'

    Changing the ?a? to /a/ fixes the memory leak. Methinks I'll keep poking.

    Update: Grump. I can't get bleedperl to compile (No rule to make target `<built-in>', needed by `miniperlmain.o') and my current perl wasn't built with debugging enabled. So I can't tell if this bug is unfixed, nor can I take it apart effectively.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Perl memory leak?
by Rex(Wrecks) (Curate) on Apr 17, 2002 at 18:54 UTC
    Printing $line and @X also seems to fix the leak. I added print $line . "\n" . @X . "\n\n" ; after the split and the leak went away.

    No explinations here, just another data point.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
You don't even need @X
by RMGir (Prior) on Apr 17, 2002 at 20:17 UTC
    No idea what causes it either, but it's clearly a split issue, since @X isn't even needed, although as others point out a my @X fixes it. Warped! (Although come to think of it, not using @X just makes perl use @_, so this may not mean much...)
    #!/usr/bin/perl # ## To burn up memory quickly, run this with option "death". $option = shift; if ($option eq "death") { $line = "1::"; } else { $line = "1:"; } my $i=1; while (1) { split(/:/, $line); # this sucks up memory too if you replace that line with # these 2 #@X = split(/:/, $line); #@X = (); if(!($i++%3000000)) { # a good place to break point print "$i\n"; } }

    --
    Mike
Re: Perl memory leak?
by mattr (Curate) on Apr 18, 2002 at 13:43 UTC
    Would Devel::Leak show anything? No access to a debug-built perl here.