I just hit this and thought I would share.

I have been debugging this for way too long and this is the results!

Situation: One of my apps (Windows service no less) is leaking 4k of memory at a indeterminate rate.

Cause: foreach $session (%SESSIONS)

In this loop I evaluate the session info in the hash key and decide if the session is still valid, if it is not I kill the session and delete the $session key of the hash. Look ok so far?

Wrong! every time through the loop I leaked 4k for every past deleted session!!! the delete was not acting on the key, the data in the keys was gone but there were still keys around

Fix: foreach $session (keys %SESSIONS)

I'm not sure why I was leaking every time through the loop, however there may be reasons I don't know about, but folks, WATCH THE KEYWORDS! this took quite a while to debug just because it LOOKED right. If someone knows why this would leak mem (I was only evaluating the data in the hash, the only changes to the hash was when I would try to delete a key) I would love an explination.

"Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Replies are listed 'Best First'.
Re: Newbie(and notso newbie?) warning!
by runrig (Abbot) on Nov 13, 2001 at 04:17 UTC
    If you were using warnings you should/would be getting these sorts of warnings:
    Use of uninitialized value in delete at Attempt to free unreferenced scalar
    What's funny is that when I run this bad code:
    %hsh = qw(a 1 b 2 c 3 d 4); for $key (%hsh) { delete $hsh{$key}; } $num = %hsh; print "$num\n";
    I get alot of warnings then a core dump. If I add 'use warnings' (or -w or $^W = 1), I get a few warnings and I don't get the core dump. Are you using strict and warnings? My guess is no, since if you were that line would be:

    foreach my $session (%Sessions) ...

      Ahh, the code was just my typing in the here, I did use my $sessions and I am running with strict and warnings.
      use strict;
      and run perl -w script.pl.

      Since coming here I haven't dared not to.

      There were no warnings and no crashes.

      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Newbie(and notso newbie?) warning!
by chromatic (Archbishop) on Nov 13, 2001 at 06:19 UTC
    Depending on how you retrieved the session from the hash, it's likely the values of the hash were being used as keys. That gives you hash autovivification, where you're actually adding new keys to the hash. That could quite easily eat up memory.
    foreach my $key (%hash) { my $session = $hash{$key}; # autovivification here if (should_delete($session)) { # receives 'undef' delete $hash{$key}; } }
    That's just a guess, without seeing more code.
      Thanks, this was my suspicion too. I didn't know how to say it quite like that, but I was suspecting that every time through I was adding keys instead of checking values.

      I really wish I could post more code, however I have to get all posted code (work code anyway) pre-approved by several people for security reasons (namely, working on products that have yet to hit the market). This makes it more of a pain than it's worth.

      Thanks again.

      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!