This is a usual problem you are going to get if create an atomselection
inside a loop. Once you are done with you $sel variable in the for loop
delete with "$sel delete". That should solve your memory issues.