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

I've found that a recursive subroutine seems to always leak some memory in linear relation to the amount of recursive calls. It's not strictly a 'leak' in that the memory used is only as big as the largest number of calls used.

Even Data::Dumper does it if it's not using it's &Dumpxs method, so it looks like a perl 'feature' to me.
Is this a known problem, and is there any work-around?

Replies are listed 'Best First'.
Re: Recursion Problem
by dragonchild (Archbishop) on Sep 27, 2001 at 17:46 UTC
    Well, you have to get memory able to handle the largest stack that happens. Perl tends to keep whatever memory it was allocated, just in case it needs it again. The thinking is that if you went 15-deep once, you probably will want to go 15-deep again.

    It's a known "problem", but I don't find it a problem. Perl maximizes speed by maximizing usage of memory. RAM is cheaper than human cycles. :-)

    As for a workaround, I suppose you could edit the Perl interpreter to have it release memory it doesn't need anymore ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      It's not only that Perl does not free the memory.

      Many (all?) unix implementations can only grow the process user space, never shrink it. In fact, I think that Windows* works the same way...

      This is one reason to design fork()ing servers. You can have a relatively small process waiting for requests and then fork() to serve each one. As the childs terminate, their whole memory allocation is returned to the system.

      It is often useful to look at the "Resident Set Size" vs the raw size of the memory allocation. The RSS is the ammount of pages/bytes that is kept in real memory and does not go out to paging/swapping. Over long periods of time, this indicator tells you how much of your allocated memory is really being used (referenced).

      However, a recursive call is not necesarilly a strain in the memory subsystem, unless you have a huge number of calls. If this is the case, you should try to reduce the memory footprint of the subroutine by using as little variables local to the subroutine as possible and passing the least arguments possible to save on stack space.

      Hope this helps...

        Actually, Windows NT has some Proc shrinking abilities and Win2K has quite a lot. If I recall from my days on the Win2K dev team, you actually have to USE the API's in YOUR code, which is why most programs just keep growing.

        "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
      Sorry, but I do find it a problem.

      Though not because I care about memory. It is because IMNSHO my $foo if 0; is a bug.

      (This bug is caused by exactly the optimization that you speak of.)

Re: Recursion Problem
by sparky8342 (Acolyte) on Sep 27, 2001 at 23:14 UTC
    Thanks everyone,
    I'm learning lots about how perl uses memory :)

    It looks like I have to just try and re-write my app to use as small an amount of memory as possible, and accept that there will be these recursion 'leaks'.