Daily iOS
Memory and ARC

autorelease buildup in loops

A for loop that runs fine on up to 5,000 records crashes after a memory warning above that. Inside the loop you slice and concatenate strings and repeatedly convert with String(format:) and NSData. The hint is 'autorelease'. Explain the cause.

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The temporary Foundation objects created in the loop pile up in the autorelease pool and stay there until the current run loop turn ends, so peak memory grows in proportion to the record count. Pure Swift values disappear at the end of their scope, but values that go through ObjC bridging land in the pool.

Share a question