Fix garbage collection bug.

Fixes an issue where garbage is being collected multiple times in a
row causing memory issues.