Reputation: 2694
I'm using ARC. I have UINavigationController to push and pop. It happens that one of the ViewController is a huge scrollview holding up 100MB. After popViewController, the ViewController that contains the scrollview is supposed to release. NSLog shows that dealloc was called. However, the 100MB memory is still occupied. Is it normal?
If dealloc of the viewController get called, does it mean that it's retainCount is already Zero and I'm not leaking the viewController?
Thanks in advance
Ref: Memory not released when popViewController Called APURV is suggesting there is a cache mechanism of iOS that holds up memory. Is that true?
EDIT:
Memory usage was observed from Debug Navigator in Xcode
EDIT2:
I just tried running exactly the same code again. But this time, I have Personal Hotspot of my iPhone ON and have it downloading something continuously. I then run the app again with the same code. I see this:
The spike in the middle is the 100MB scrollview, now if I popViewController, it goes back to 12MB. It makes me think that there's really a cache going on in iOS and is depending on memory availability. When it's out of memory, the cache will be washed away, also, release will then happen immediately.
I will also try to learn to Instrument and post result here later.
Upvotes: 2
Views: 3104
Reputation: 811
calling popview controller removes the reference of navigationcontroller from the controller being popped.However if your view controller is still not getting released that means someone else is holding it.
Use the debug memory graph option in xcode as it shows the incoming and outgoing references to the objects currenlty present in the memory. All you need to do is to identify the references and make them weak. I dont suggest making unowned because then you have to be completely sure that the object will be present when you will use it. So weak is always a safer practice.
Hope this helps someone.. !!
Upvotes: 0
Reputation: 8180
Your code might be totally correct. I've seen that many times the configuration of the memory bar in XCode can be misleading, maybe this is happening to you.
Your need to open the Edit Scheme... dialog and disable the Enable Zombies option in the current scheme configuration, under Diagnostics.
Upvotes: 2
Reputation: 5374
It's not necessarily the view controller itself that is kept in memory, but rather the heavy objects it contains.
You should have a look at Instruments to find out what's going on. If you use the "Allocations" instrument, and run your app, you'll see a "Mark Generation" button on the side.
If you hit it before and after pushing in and popping out the view controller, you should see different "generations" and the consequent memory growth. When expanding each generation, it will show you which objects are taking that memory, and the small arrow next to each will show you where they were allocated, and help you track down which references are keeping them from being released.
Upvotes: 4