views:

286

answers:

1

I have a navigation controller-based application with 5 view controller inside. When I push a view controller I allocate some memory and when I go back with popViewController my delloc() method is correctly called. I'm sure that the dealloc is called in the right way for every view controller I push and pop.

Nevertheless when I run the application with Instruments (Start with performance tools -> Object allocations, Leaks) there is a strange behavior for me. When a view controller is pop the memory usage does not decrease, to be exact it does not decrease as expected: when I start the application it use 950 KB, then I push the first view controller and the memory usage increase up to 1,56MB, finally I pop the view controller and the memory usage is now 1,55MB.

Is this behavior right?? I'm sure that every dealloc method is correctly called whenever I pop a view and the Leaks instrument does not show any memory leak. I guess that the operating system is "retaining" in some way the view so that to second time I push the same view controller the loading process is much more fast...

Could someone confirm that this behavior is right? Thanks

See this Screenshot from Instruments

+1  A: 

Hi

This is as expected. The memory handling rules of "you are only accountable for objects you did alloc, copy etc. on" applies here as well.

When you push stuff on to the navigationController I assume you do it like this:

MyController *myCon = [[MyController alloc] init];
[self.navigationController pushViewController:myCon animated:YES];
[myCon release]; //You have alloc and release.

The navigationController is often handling a hierarchy where a user drills down a data set, and up again. By holding onto your controllers when memory is plenty is the navigationControllers way of saving having to instantiate the controller again 5 sec. later when the user taps "back". You can see this because dealloc never gets called, but viewWillAppear and viewDidAppear are called when you back up. If memory is lacking the navigationController will start to release controllers on its stack.

But! make sure that going back and forward does not result in the viewControllers being instantiated again and again, this will make the memory footprint grow and there is a leak. The navigationController should notice that it already has the viewController in its stack and simply display it.

You should be able to move through all the views and if they "fit" in memory, the app should never increase its memory footprint from here on out.

RickiG
Thank you for your reply. I instantiates the view controller as you described. If I understand well so I can assume that when back button is tapped, the navigation controller will "retain" my controller even if the dealloc method has been called? Is right? because my dealloc is always called (I use an nslog).The point that I don't understand very well is how it can retain my controller if has been already deallocated? or is the view inside of the controller that is retained?
ggould75
How do I make sure that going back and forward does not result in the viewControllers being instantiated again and again? When I click back on one of my views, it changes the navigationBar but not the view. Why?
MattDiPasquale