3

I've noticed when opening multiple instances of a view, my memory continues to climb with the more views that the user opens. If the user starts to hit back the memory usage drops with each view controller closing. However, depending on whatever the user is doing he can open 20+ view controllers, how can I manage the memory utilization? Keep in mind I need all those views loaded in the background so they can be quickly loaded when the user hits back

Heres how I'm creating each instance:

let storyboard = UIStoryboard(name: "Storyboard", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("FriendPage") as! FriendVC
self.navigationController!.pushViewController(vc, animated:false)

How can I manage the memory utilization?

2 Answers 2

10
+50

A navigation stack keeps all the view controllers loaded in memory. That's integral to the way it works.

As Mr. Beardsley says, you can set up your view controllers to free their large data structures in your viewDidDisappear method (including setting image views to nil) and then reload them in viewWillAppear. If you make sure everything is cached to disk it should reload quickly.

To go beyond that, you'd need to forgo a navigation controller and create your own parent view controller that displayed a series of child view controllers. You could make the parent keep track of the navigation path the user followed and save state data for each view controller to disk, and then on the user pressing the back button, re-invoke the previous view controller and reconstitute it from it's saved state data. As long as everything is loaded from disk and not from the network you should be able to get near instant display of each screen when the user presses the back button.

This would require a fair amount of custom work on your part but shouldn't be that hard.

There are methods like 'transitionFromViewController:toViewController:duration:options:animations:completion:' that let you create custom transitions between child view controllers. You should be able to easily create whatever transition effect you want.

By saving a list of the view controllers that the user visited and a block of state data needed to recreate each view controller from disk you should be able to simulate a navigation stack while only having one child view controller active and in memory at a time.

Before going down this path, though, I would suggest looking at your user interface and seeing if there is a way to limit the depth to which the user can navigate. You could add some sort of limit to the depth of the user's navigation. The details would depend on your app design.

1

Unless you are running into memory pressure issues, I would not worry about it. How large does your memory usage grow when you navigate 20 levels deep in your view controllers? If you are running into issues, then you would have to save the state of the previous view controllers to persistent storage, and then set your current view controller as the root. When you go back, you'd have to reinstantiate the view controller and restore the state.

A middle of the road approach might be to have view controllers release any image, or other large binary data, when a new controller is added to the stack. When you navigated back, the view controller would have to reload the data from disk or the network.

1
  • can you please provide an example?
    – user5130344
    Oct 5, 2015 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.