12
votes

I'm attempting to download a large number of images using AFNetworking 2.5 and stream them to disk. According to the memory monitor in Xcode, this is causing unbounded memory growth (and eventually memory warnings and force quits) - but profiling the memory usage using the allocations instrument shows the memory usage to be stable.

I'd like to think Xcode is just wrong here, but then why would my app be getting killed by iOS?

Instruments shows this - the peaks are Core Data queries, then the rest is the images downloading - peaks at about 9.5MB, the rest sits at about 8.5MB

Instruments

I've also tried Heapshot Analysis - which shows a tiny bit of growth but nowhere near the amount reported by Xcode

Heapshot Analysis

Xcode's memory monitor shows this - growing by multiple MB per iteration.

Xcode Memory Usage

Is there any way to get Instruments to show me whatever Xcode is seeing? Or is there a better instrument to use to find out where all this memory is going?

Thanks!

1
When you run the app through profiler, I believe it defaults to a release build. I wonder if when you're running it through Xcode, whether you're building a debug version. Check your schemes and make sure the two are building the same version. - Rob
Both "Run" and "Profile" are set to debug in the scheme editor. Unless there's some other place that it could be set? - Nathan Gaskin
No, that's precisely where I was thinking about. It's just common to see release build for profile (and to see different behavior than debug builds as a result). And, of course, I presume that in Instruments, under the Allocation Tool's "Recorded Types" setting in the "Record Options" tab, that you're recording all types? Very, very curious. A heisenbug. - Rob
One final thought: I have had vexing situations where Instruments was inexplicably profiling an old version of the app. I'd suggest you actually not only do a clean build, but actually remove the app from the device/simulator before rebuilding. - Rob
I've tried deleting the app with no luck. Also tried changing the build type to debug in the Cocoapods schemes for AFNetworking etc and profiling again, still the same behaviour. Weird! - Nathan Gaskin

1 Answers

25
votes

According to the memory monitor in Xcode, this is causing unbounded memory growth (and eventually memory warnings and force quits) - but profiling the memory usage using the allocations instrument shows the memory usage to be stable.

Believe Instruments and the Allocations information - not the memory monitor in Xcode. The memory monitor graph is completely irrelevant. Ignore it.

This is not because the memory monitor in Xcode is useless or wrong. It is because memory management is completely different for a debug build than for a release build (Instruments uses a release build). This is especially true in Swift (you don't say whether you're using Swift).

Observe memory usage only on the device and only in a release build. Otherwise, you'll be completely misled.