1

a tab bar based app with 5 tabs switching option...how i am suppose to manage memory efficiently?

switching between tab are very frequent i am how to manage this scenario?

or

all tab will remain active no matter what? but thats a bad option...

please describe in detail regarding tab bar memory management

Hazel Sophie
  • 1,345
  • 2
  • 9
  • 8

3 Answers3

8

Let UIKit handle it. You shouldn't worry. UIKit will unload views as it sees fit (and you get told about that in viewDidUnload of your view controllers).

So for instance:

  1. You start on tab 1. Tab 1 is the only view controller whose view will be loaded.

  2. You tap on tab 2. Now tab 2's view controller will be loaded and tab 1's view controller is still around.

  3. More time goes on, you tap on other tabs which loads other view controllers.

  4. UIKit notices that memory is running a bit low or it just wants a bit of a tidy up (you have no control over this). So now it will go and unload some of the view controllers' views (but obviously never the one you're currently viewing).

You should of course be a good citizen and release anything you hold onto in your view controller in viewDidUnload that you can easily create again when the view wants to be loaded again.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
2

You don't need to worry about it, unless you're in a low memory situation, in which case the view controllers' views may be released, and you just need to correctly implement:

didReceiveMemoryWarning

and

viewDidUnload

See Apple docs here and here for details.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
1

All View Controllers associated with a Tab Bar Controller are retained by the tab bar controller, but if you're using intense amounts of memory, you can release objects or resources used by your view controller when viewWillDisappear: or viewDidDisappear: gets called. And recreate / reallocate those memory hogging objects when viewWillAppear: or viewDidAppear: get called when the user clicks it into view again.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    This is the wrong answer. UIViewController has methods fired in low memory situations, you only need to free up memory in these cases. Releasing and recreating objects when a view controller's view appears/disappears is likely to result in a poor user experience. Think of the situation when a modal view is displayed or another view is pushed on the nav stack - you wouldn't want to release objects in those cases. – Ashley Mills Jan 13 '12 at 15:37
  • Hi Ashley; no, I don't think this is a wrong answer. The guy asking the question was asking for memory management considerations and I was providing some detail. That being that the view controllers (and whatever they've allocated / retained themselves) are retained in memory by the tab bar controller. Hopefully he will implement code to support true low memory conditions via the `didReceiveMemoryWarning` and `viewDidUnload` methods. +1 to you for your answer though, since you're a good sport. – Michael Dautermann Jan 13 '12 at 15:43