2

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 enter image description here

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: enter image description here

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.

Community
  • 1
  • 1
John
  • 2,672
  • 2
  • 23
  • 29
  • Are you using a Storyboard to pop your viewController or you are doing this by code ? – Lapinou Mar 23 '14 at 17:45
  • @Lapinou, I pop by calling [self.navigationController popViewControllerAnimated:YES]; – John Mar 23 '14 at 17:47
  • If dealloc is called that means that retain count reached 0. Indeed. Where are you seeing those 100MB still occupied? Instruments? – Merlevede Mar 23 '14 at 17:48
  • Maybe this post can help you: http://stackoverflow.com/questions/6881222/uiviewcontroller-not-being-dealloc-ed-under-the-new-arc-memory-management?rq=1 – Lapinou Mar 23 '14 at 17:49
  • do you hold a pointer to the scrollview anywhere else than in your viewController? Did you swizzle the dealloc of any UIScrollView superclass? – Nicolas Manzini Mar 23 '14 at 17:52
  • @NicolasManzini, no the scrollview is only used within that specific viewController. I used UIScrollView as it, didn't subclass or touch anything in superclass – John Mar 23 '14 at 17:56
  • @Merlevede Please see the edit. The memory usage was observed from Xcode – John Mar 23 '14 at 17:56
  • @Lapinou I hope it's really cache as your post suggested. The 100MB just worries me so badly cause I'm building a Music App that I am using couple of hundreds of MB in other viewControllers.. – John Mar 23 '14 at 18:02
  • actually maybe it's the same problem as with MKMapView, simulate a MemoryWarning notification and watch if the memory gets released. – Nicolas Manzini Mar 23 '14 at 18:03

3 Answers3

4

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.

enter image description here

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.

enter image description here

matehat
  • 5,214
  • 2
  • 29
  • 40
  • 1
    Thanks Mate. I didn't use instrument before, this is a good opportunity for me to learn. I'll try your test and let you know the result – John Mar 23 '14 at 17:59
2

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.

enter image description here

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • Ar.. Good point to notice. But I checked and I didn't select the Enable Zombie Objects option. Thanks though. – John Mar 23 '14 at 18:09
0

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

Pranav Gupta
  • 741
  • 9
  • 10