Why ARC doesn't release memory when popViewCon

2019-08-09 12:58发布

my project use ARC, and when i have to show a view with navigation controller i do this:

ShareViewController_iPhone *share = [[ShareViewController_iPhone alloc] initWithNibName:@"ShareViewController_iPhone" bundle:nil];
[self.navigationController pushViewController:share animated:YES];

and i can see in xcode that the memory is increased of a certain value, then when i dismiss the view i do this:

[self.navigationController popViewControllerAnimated:YES];

but when the view is closed, the memory doesn't decrease, how i can do it?

4条回答
放我归山
2楼-- · 2019-08-09 13:06

ARC will clean the memory on its own cycle (it will collect all the objects whose reference count set to "0" means who are ready for deallocation), we can't control this. It is managed by the operating system.

So when you are popping a view controller, it doesn't deallocate it from the memory immediately. It can take time depending on the other memory usage/memory availability.

One more case, you need to check if on every push the memory allocation is increased by a certain value, then there will be definitely something wrong in your code.

查看更多
贼婆χ
3楼-- · 2019-08-09 13:06

When you pop a ViewController and it don't release, it means have a reference cycle in your controller. You must check delegate if have, block, weak, strong reference.

  • Your delegate must be weak reference.
  • If use block. The self must be weak reference: __weak id weakSekf = self;

Hope these information will help you. :)

查看更多
霸刀☆藐视天下
4楼-- · 2019-08-09 13:21

When you pop the viewcontroller, it is marked for deallocation (i.e it's reference count becomes 0 unless referenced by another object). but not deallocated immediately. Deallocation as far I have seen is mostly random!

查看更多
Deceive 欺骗
5楼-- · 2019-08-09 13:23

The view controller here must be retained for a while by the navigation controller itself to create the animation. The deallocation does happen immeadiately after the reference count drops to zero. Try to pop the view controller without animation (e.g. animated:NO) and see for yourself if the deallocation happens right away.

查看更多
登录 后发表回答