On 04/05/2013 11:22 PM, Jerrad Pierce wrote:
at each level of recursion. What seems to be the case though is that when we 
start going bac
up the stack that memory doesn't seem to be released at each pop. If, say, at 
max depth
500mb of ram has been allocated I don't see that released at any point except 
for when
perl exits and then of course it is all released at once. Or at least that is 
what
seems to happen.

Perl doesn't release memory, it keeps it for reallocation.
This is (was?) one of the issues one had to be aware of with mod_perl.

this is true for almost all programs. releasing ram back to the system is tricky on unix flavors. it can only happen at the top of the ram space by using the brk() system call. this means your process has to not only free its ram it has to consolidate it and check when it can release it to the system. this needs special and likely much slower malloc/free calls. it can be done with careful and special ram allocation but with the massive number of small mallocs that perl does (and most langs do) it is a practical impossibility.

but remember, this is virtual ram and not physical. if perl stops accessing ram it has freed as it doesn't need it anymore, it will swap out naturally. it is still more resource intensive than an iterative solution but not as bad as you (the OP) thinks.

recursion is a great concept and i use it for template parsing in Template::Simple. i don't expect template structures to nest 100 deep! :) in that code an iterative version would be harder to code than a simple single call to parse the nested sub-template.

but recursion 100's deep would bring up serious speed and resource issues and i would explore iterative solutions from the start.

others mention tail recursion and that is only good if you make the recursive call last. that isn't always possible and when you can do it, you may need to do some entangled code to force that to happen. for example in my template module, i need to do work after the recursive call and it would be very convoluted to make that become a tail recursion.

one thing i dislike about tail recursion is how it is used just for simple looping. maybe it is as fast if done right but it feels like the everything looks like a nail when you have a hammer syndrome. loops are fine as they are.

uri



_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to