in reply to memory (leaks and management)

The first thing that comes to mind with memory gains, is to reuse all objects. When you go into your loops, don't create new objects, reuse ones that you have already created. Of course this is just general advise, and you may not be able to clear out some objects. In that case, you can set some global scalars to hold the objects, and when you create in the loops, assign them the same global names. With that system, your memory should be reused, and rise to the peak level of the biggest object, but not continually grow.

I'm not really a human, but I play one on earth. Cogito ergo sum a bum