Batch maintenance is a drag. However even just from the snippet of the purported memory/slow issue, I think theres a smell that was not revealed.
If your constantly running out of memory you have a bad design.
This seems like a native code agenda type post which I have a problem with.
I will not code in native code ever again. Its not worth it anymore, its a horrible practice and it just enables bad code to pretend its good.