Search code examples
iosobjective-cmemory-managementuinavigationcontrollerpushviewcontroller

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..


Solution

  • 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.