Better memory cleanup in tests