Why ARC is not deallocating memory after popViewController

本秂侑毒 提交于 2020-01-11 17:12:40

问题


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?


回答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




回答2:


Try to avoid using strong properties for IBOutlets.




回答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?




回答4:


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.




回答5:


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




回答6:


I think you get an error, when you try to pop the view controller because the navigation controller does not have a valid reference to the view controller, as it was released after you pushed it.




回答7:


Nil the popover on dismiss.

[menuPopup_ dismissPopoverAnimated:YES];
menuPopup_ = nil;



回答8:


Make sure your viewcontroller (A) has no reference of any other viewcontroller (B) or any object it has. Incase it has then make sure that VC-B is not referencing back VC-A. If it has some reference back to VC-A then make it a weak property. Otherwise strong retain cycle will keep the VC in memory even if its popped.

  • Another thing is to check wether theres any closure in your VC, check its body if it is referencing any property or method with self then make a Capture list to avoid retain cycle as "Closures are Reference Types"
  • Check if theres any NSNotification observer you are not releasing

  • Make a print call in deinit method to check wether its deallocated.

For further understanding on memory management:

  • https://medium.com/mackmobile/avoiding-retain-cycles-in-swift-7b08d50fe3ef
  • https://www.youtube.com/watch?v=GIy-qnGLpHU



回答9:


You should use an unwind segue instead of popping.



来源:https://stackoverflow.com/questions/21398934/why-arc-is-not-deallocating-memory-after-popviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!