Why ARC is not deallocating memory after popViewController
Solution 1
When you dismiss a view controller (or pop it), it will be deallocated if you didn't make any strong pointers to it (that controller is retained by the navigation controller, or the presenting view controller, so you usually don't need to have a pointer to it when you create it and push or present it).
It will be be released if there are no other strong pointers to it
Solution 2
Try to avoid using strong properties for IBOutlets.
Solution 3
Consider reviewing whether you are referencing self
in a block. If you do, you risk holding onto the UIViewController reference after you have popped it.
For a more in-depth review of why, check out this answer: How do I avoid capturing self in blocks when implementing an API?
Solution 4
I would like to say, that my last few days were spent on searching the web for my app memory problem. I was switching between 2 UIViewControllers. One of them had a scroll view which kept all subviews on it. It turned out that that UIVC loads a new scroll view without releasing the previous one. It took me several hours to realize it.
What I did was:
Looking for any kind of deadlocks inside the app, then searching for every variable that had a strong atributte and other desperate measures. But what really worked was:
@IBAction func backBB(sender: UIBarButtonItem) {
collectionView.removeFromSuperview()
self.frontView.removeFromSuperview()
eventsPhotos.removeAll(keepCapacity: false)
symbolContainerView.removeFromSuperview()
self.myScrollView.removeFromSuperview()
dismissViewControllerAnimated(true, completion: {})
}
I manually removed some views and contents. I've done it in "Back" button but you can do this in other methods like viewWillDisappear(animated: Bool).
Once I made this, my allocation chart in the developer instruments showed the memory allocation going up and down... And it was solved...
Solution 5
If your app design allows the user to push and pop the same view controller over and over again, you may want to look at reusing the same view controller and just updating its contents each time it's pushed.
Instead of creating and destroying it over and over, create one, set up its contents and push, when it's popped, keep it around ready to be shown again. Next time it needs to be shown, update its contents and then push it again.
Comments
-
Wali Haider almost 2 years
I'm pushing and popping ViewControllers in UINavigationController.
I'm tracking the memory consumption of my app. While pushing the new viewController the memory consumption is increasing gradually, but when I'm popping the same ViewController using
[self.navigationController popViewControllerAnimated:NO];
the memory consumption does not decrease but the constant.That particular viewController can be pushed and popped by user many times which can lead the high memory consumption of app in RAM.
What should I do to optimise my memory consumption?
-
jrturton over 10 yearsWhy? Weak outlets were relevant in iOS5, when views would get unloaded, but will not cause issues now.
-
Naga Mallesh Maddali over 10 yearsYou mean there wont be any difference if we declare an IBOutlet as a strong or weak property in ARC post iOS 5?
-
Tim over 10 yearsInterested in your point @jrturton, is this documented anywhere? Also, why would you use a strong property now instead of weak anyway?
-
jrturton over 10 years@Jeff, Strong is the default, why do the extra? The point of weak properties was that they would be nilled out when the view was unloaded. The view doesn't get unloaded anymore, so there is no point. I don't know where this is documented.
-
jemmons about 10 yearsApple says "Outlets should generally be
weak
." Here's the docs. -
Jonny about 10 yearsWhile correct, I don't think this is to the point. Despite the controller itself being deallocated there are times when memory usage does not decrease even with pops, like in the question.
-
Jonny about 10 yearsCheck out "abandoned memory" and how to find it. It's not the same as leaks. developer.apple.com/library/ios/recipes/…
-
Rambatino almost 10 yearsThe question doesn't refer to a popover.
-
Harsh over 7 yearsThis is outdated now. Since iOS 7, am using IBOutlets as strong and it doesn't make any difference.
-
Harsh over 7 yearsWhy is this answer?
-
Ribelyn Punk over 7 years@Jonny, as i'm using navi controller too, but i got list of pages which are handled in it, which puts lots of load on stack, which occurs crash in the end, is there any way or technique to free the stack load
-
Itai Spector almost 7 yearsAlthough you solved it for your personal needs, that doesn't seem like a clean solution. I think there's some deeper key that needs to be figured out.