in reply to Clean Code - What a mess...

First of all, strict and warnings could help you a lot... making sure all of your variables are defined. Whenever you have variables used only in a loop, it is good to define them with my in that loop, so they get cleaned up when they go out of scope...

looking at your code I am guessing your leak is in

$spy[$ct]=$global[$x];
$ct just keeps getting incremented, so @spy just keeps getting bigger and bigger, and never clears out. There may be other things, but that is what I see from a quick look.

                - Ant
                - Some of my best work - (1 2 3)