|Perl: the Markov chain saw|
Nested Calls, Recursion and MEMORYby robdog (Novice)
|on Dec 19, 2002 at 20:35 UTC ( #221243=perlquestion: print w/replies, xml )||Need Help??|
robdog has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a simple recursive routine which file globs a directory, saves all the entires in an array, and recursivly does the same for any sub dirs.. i noticed that a "TON" of memory was consumed, during the process.. and apon return , using Devel::Leak, many many blocks were still allocated..
Calling the same function again, did NOT accumulate any more blocks.. Ok, maybe Perl is reusing the memory.. fine.
So i thought, a recursive function, is just a nested call onto "ITSELF". Would the same memory consumption apply if Function A called Function B, and Function B called Function C? These are just nested calls, but not onto itself. I got back ZERO blocks allocated after Function A. As i wanted. As _should_ be.
So i assumed that nested calls were OK.. but just not on itself. (due to a memory not being immediately released)..
So Obi Wan, i now ask why. Why is memory allocation different when a nested calling function , calls itself, vs calling another function. Does perl have special code to deal with recursion?
Any and all replies are appreciated...
Below is my test code...
System : Win2k, ActiveState 5.6.1.
Back to Seekers of Perl Wisdom