Pick the fix
Resizing 10,000 photos in a loop uses gigabytes of memory until the loop ends. Best fix?
Hint 1 of 1
When are autoreleased objects freed?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Wrap the loop body in autoreleasepool { }
Correct. Each iteration's autoreleased images are freed right away.
Why not the others?
- B Dispatch each iteration with DispatchQueue.main.async. That moves work to the UI thread and doesn't bound memory.
- C Set the image variable to nil at the end of each iteration. The autoreleased objects are still waiting for the pool to drain.
- D Lower the JPEG quality. Smaller files don't change how long autoreleased objects live.