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. | [reply] |
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...
| [reply] |
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!
| [reply] |
| [reply] |
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'.
| [reply] |