Prashanth Rajagopalan
Prashanth Rajagopalan

Reputation: 726

UINavigationController pushviewcontroller memory management

I have a UITableiew listing n number of contacts and from Table view delegate didSelectRowAtIndexPath I am navigating to a 'Contactview' UIViewController by using UINavigationController pushviewcontroller.

For an instance if I navigate the first contact to Contactview, Live Bytes memory goes up from 1 MB to 3 MB. Then when I tap on the back button the viewcontroller delloc method is called but the memory still stay around 2.95MB to 3MB . My question is when the viewcontroller delloc method is called the memory of the viewcontoller should be released right ? Am I wrong anywhere ? Please suggest me if I am wrong. And I am using ARC project.

Thanks in Advance..

Upvotes: 1

Views: 3028

Answers (5)

Daniel Martín
Daniel Martín

Reputation: 7845

If you push your navigation back and forth and you see memory climbing unlimitedly, you have a memory management problem. Even with ARC, you may have abandoned memory. You can detect it using the Allocations template in Instruments.

  1. In Instruments, put the application in a well-known starting state (for example, showing the table view).
  2. Click Mark Heap button under Heapshot Analysis.
  3. Navigate your controller back and forth once.
  4. You will see a small increase in memory usage in the allocations graph. This is normal, internal caches may be storing some information.
  5. Click the Mark Heap button again.
  6. You will see a number of objects in the Still Live column.
  7. Repeat steps 3-6 many times and see if there are "still living" objects after every iteration.

If there is an almost constant number of still living objects in each heapshot, click the right arrow button in one of the heapshots and you will see all the objects that are still living. Look for objects probably created by you, select one, expand it, and select its memory address with a simple click. Then click the Extended Detail button to see a stack trace showing where the object was allocated. With this code context I'm sure you will understand why your memory was abandoned.

Upvotes: 5

Zeke
Zeke

Reputation: 134

You might own a strong reference for the view controller elsewhere in the code. You should be sure if it's really deallocated... If any other object has reference to it beyond the navigation controller it won't be deallocated. Try override dealloc. (You could override dealloc in an ARC project as well, you are only not allowed to use retain count manipulation calls.) To be sure if dealloc is called put some logging or something debugable code into that method.

Upvotes: 0

Jonathan Zhan
Jonathan Zhan

Reputation: 1893

UIImage caches images for you as an optimisation, so this is expected behaviour.

If you wish to confirm that this is the case, for peace of mind, you can force a low memory warning (Under the Hardware menu for the simulator). This should get UIImage to throw out its cache.

You can also use this private method, but of course throw it out before submission.

[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];

Upvotes: 0

shuvo
shuvo

Reputation: 705

Did you check retainCount? is that showing your desired value?

Upvotes: 0

NHS
NHS

Reputation: 409

See.. one thing ARC will release it the contents some where in future right.Its Automatic right.. how can expect the ARC to do the Gatrbage collection after class will disappear.It might take time to free the memory.

Upvotes: 0

Related Questions