Gerard
Gerard

Reputation: 4848

How to limit iOS app memory usage

Ok so this is a pretty vague question but let me give you some background:

I have a straight forward app so far - about 5 screens each of which will hit the database and display some information on the resultant view. The objects aren't huge, about 15 attributes some of which would be NSSets with 3-4 objects contained.

I've noticed that by the time I'm on my 5th screen, memory has jumped to nearly 6mb which seems huge to me (though I could be wrong).

I do notice a 1mb jump every time I do a DB call like NSFetchRequest which uses the usual NSManagedObjectContext, NSPersistentStoreCoordinator, etc as you'd expect.

Do I need to close the context when I'm done with the DB or something like that? I remember such Persistence Contexts causing huge memory problems back in my Java days and since there's nothing else going that (I believe) can really be taxing the app thats where I'd guess the issue is.

I'm using ARC and CoreData.

Any insight into this would be really appreciated.

Update

So I used Instruments as recommended (great tool btw) and it looks as though the problem is caused by one screen with about 30-40 UILabels (resulting in a use of about 4MB). Seems like a lot of memory for UILabels or am I completely off?

Upvotes: 0

Views: 691

Answers (1)

Tommy
Tommy

Reputation: 100652

Every UIView has a CALayer. So every view ends up caching its contents. Supposing your 30–40 labels ended up filling the screen and you were on an iPhone 4 then you'd expect them to have a memory footprint of at least 960*640*4 = 2.4mb. I'm willing to bet they occupy more room than that.

If you're suffering from it, ignore your instinct to think of that as a huge waste, because:

  • under iOS 5 and earlier, if a memory warning occurs then any off-screen view controllers will release their views; and
  • under iOS 6 (and presumably onward), if a memory warning occurs then the layers attached to all off-screen views release their contents.

So it's usually the case that the memory is spent only while it's available — it's a simple matter of not ignoring a resource just because it may not always be available.

Upvotes: 1

Related Questions