Jordan Smith
Jordan Smith

Reputation: 10378

iOS - Memory Management: Where is all this memory going?

I've got a fairly basic app that I'm updating - and as part of the update I'm implementing memory warning management.

When the memory warning method gets called, I release any view controllers not in use (and these in turn release any of their objects). Everything seems to work fine, there are no leaks etc as far as I can tell.

What isn't working:

Using the 'allocations' instrument, there is a lot of memory that isn't being released when a simulated hardware warning is called. Here's what I do when testing:

1 - start the app - this is the original jump in memory shown below.

2 - add a new view controller - this is the second spike.

3 - Return to the main view controller and simulate a hardware memory warning - this is the (small) drop in memory towards the end. This warning should completely release the additional view controller and associated objects.

enter image description here

How can this memory be released - or what am I doing wrong? Any pointers would be much appreciated - thanks!

-

EDIT: Thanks for all the answers so far! Although unfortunately I still haven't been able to solve the issue yet. Furthermore, the memory weirdness only seems to occur when using modal view controllers.

I've noticed that I actually have a background loading method for the extra view controller called when the app launches, to make things run smoothly. This indicates that the second spike in memory is completely due to something other that the view controller - maybe animations or something? Anyhow the problem still remains - what is this extra memory being used for, and how do I release it when I need to?

I could potentially create a mini project that exhibits the behavior if it would help. Thanks :)

Upvotes: 1

Views: 702

Answers (2)

hwaxxer
hwaxxer

Reputation: 3383

Are you really sure you are not retaining your UIViewController or its objects? You shouldn't have to simulate a memory warning to see a decrease in allocations after releasing your controllers. I attached a screenshot showing how my application looks after pushing and popping a UIViewController three times, using a UINavigationController.

Allocating & deallocating

EDIT

To answer your comments: UIViewController belongs to UIKit and is not thread safe, which means you should not be creating one in a background thread. This could potentially be the cause of the memory leak, since it will not be added to the main autorelease pool.

Upvotes: 2

Alex
Alex

Reputation: 1741

There's a really-really great screencast about memory analysis with Instruments from WWDC 2010. That's how I learned how to track similar issues.

You should check out http://developer.apple.com/videos/wwdc/2010/. To be specific http://developer.apple.com/videos/wwdc/2010/?id=311.

Upvotes: 2

Related Questions