Search code examples
iosmemorytabsnavigationmaster-detail

When does iOS allocate memory and deallocate memory? (with respect to the default behavior of navigation and tab controllers)


I'm new to developing for iOS. I've seen a lot of documentation about controlling memory management programmatically, however I'm curious about the default way iOS handles memory when navigating between:

  • Tabs
  • Master-Detail Views
  • Master-Detail Views with Tabs

(Note: When I say a Master-Detail view, I am basically referring to a Navigation Controller. I'm just calling it a "Master-Detail View" to define two different levels of the Navigation Controller hierarchy, so its easier for us to discuss)

I will go through each of the 3 topics with the questions I have:

Tabs

  1. When the app first boots up, does it load all of the data into RAM for each of the tabs, or does it just load the data for the tab that is initially displayed?
  2. If the answer to 1 is no: Whenever you switch from one tab to another, does it deallocate the data of the tab you switched from?
  3. If the answer to 1 is yes: If the app needs to load data from a database in each of its tabs: does it just load the user interface / local functionality into RAM, and only load from the database data when that tab is selected (when this data is needed)?

Master-Detail view (in the case where the Master is a UITableViewController, and the Detail is just a UIViewController and they are embedded in a Navigation Controller)

  1. When you load a Detail View, then go back to the Master View, does the data from the Detail view still remain in RAM?
  2. When you load a Detail View, then go back to the Master View, then go into a different Detail view, does the first Detail view's data stay in RAM?

Master-Detail Views with Tabs (in the case where the Master is a UITableViewController, and the Detail is just a UIViewController and they are embedded in a Navigation Controller)

  1. When you load a Detail View, then switch to a different tab, is the Detail View's data still in the RAM?
  2. When you load a Detail View, then go back to its Master View, then switch tabs, is the data from the Detail View still in RAM?

All of these questions are referring to the default behavior of iOS.


Solution

  • A couple of thoughts:

    1. Diagnostics: The easiest way to diagnose what's going on is to add logging statements to your child view controllers. For example, if you add the following line to the viewDidLoad and dealloc methods, you'll be notified as controllers and loaded and deallocated:

      NSLog(@"%s", __FUNCTION__);
      

      Then, armed with view controllers that will log as they are instantiated and deallocated, you can use these controllers in any combination of container controllers (tab bar controller, navigation controller, split view controller, etc.) and you can easily see what's going on yourself.

    2. Regarding the specific container controllers you asked about:

      • Tab bar controller: In storyboards, tab bar controllers allocate the child controllers as you select their respective tabs (and not before), but keeps old tabs. (In NIBs, it just depends upon what you did programmatically; you can either preload them or not preload them.)

      • Navigation controller: Navigation controllers load new child controllers as you push to a particular scene, but it does not release the previous view controllers in that process. Only when you pop a view controller off the navigation stack will that controller be released.

      • Split view controller: As you perform a replace segue in the split view controller, it will instantiate the new child scene, and will release the previous child controller. And if that previous child controller, itself, was a container controller such as a tab or navigation controller, then its children will be released as well.