Don't save the product, but only the attribute. This can be done like:
At the end of the loop unset the variables you are using. The GC should deal with them.
You can even avoid loading the product and you can even avoid loading the child products if you use SQL to sum up their qty and then fetch only the result.