0

I notice that in my app, sometimes when a view or view controller gets deallocated, the memory allocated for those is not freed. What could possibly be the reason for this?

I define deinit method to verify that the view and view controller do get deallocated (deinit gets called). I said sometimes because in some other cases, when I remove a view, the memory does get freed. Another thing I notice is that when I add a view, the memory usage goes up, then I remove that view (deinit is called), the memory usage stays the same, but succeeding view additions of the same view don't affect the usage.

The view is created in a method and added by View.addSubview(UIView). In the view, there is a button that invokes View.removeFromSuperView().

Son Nguyen
  • 1,124
  • 2
  • 10
  • 24

1 Answers1

0

Try the below instead:

autoreleasepool {
    View.removeFromSuperView() // not sure why you capitalized View but repeating it here just to match your code.
}

This gives iOS the chance to deallocate autorelease objects. They'll eventually autorelease anyway, like if the system needs more memory, but this allows you to force it to happen early. This SO answer explains it in more detail.

Smartcat
  • 2,834
  • 1
  • 13
  • 25