0

I have created an app with 12 view controllers and its got a lot of graphics ( image files )

When I launch the app, I notice the memory goes up as I move through the view controllers.. once I have moved through all the controllers the memory is 530Mb

so my questions are :-

a) is this a lot of memory to be using b) should I be worried c) how can I remove view controllers from memory as I navigate round.. for example I have a view controller for on boarding , but once some clicks on skip or get started, I segue to the next view controller, so in theory it doesn't need to be loaded any more..

Thoughts ?

Im running on an iPhone 7 Plus which has 3Gbm, but want to be able to run the app on other phone models from iPhone SE onwards. however I notice the memory profile is about 50% no the iPhone SE but assume thats because its not loading the x3 images as per plus phone

1 Answer 1

1

a) is this a lot of memory to be using

lot is a relative term and whether 530Mb is lot or not completely depends on what app is doing and what kind of app it is! If its a game with rich graphics I wouldn't be bothered much but if its a plain utility app I would be concerned!

b) should I be worried

Memory getting accumulated/increasing as user loads various screens of app is a very common scenario. You should be bothered if it keeps increasing and never comes down. Every time a VC loads it might load, heavy graphics with it or might allocate large amount of variables and consume lots of memory to perform its function. Which is quite fair. But once user pops the VC from applications navigation stack all the memory allocated by the VC should be returned and total memory consumed by the app should come down. Ideal memory foot print would look like a wave where it reaches peak and comes down once VC pops out. If thats not happening you are in trouble :)

c) how can I remove view controllers from memory as I navigate round

  1. Use proper navigation techniques. Don't keep pushing the VC's to navigation controller stack unless you actually need that VC instance to be retained in memory. All VC's pushed to Navigation stack will continue to be kept in the memory till either user kills the app or iOS decides to kill the app on receiving memory warnings.

  2. Write deinit/dealloc in each VC and make sure it gets called every time user pops the VC by either tapping back button if its pushed or by dismissing VC if its presented modally. Ensuring each VC's deinit gets called is the best way to ensure VC does not hold up any unnecessary memory.

  3. Make sure none of your VC has code which results in retain cycle and retains the objects in memory forever. Example : If your VC declares a block and holds the strong reference to block and if you pass self to block your block and self will never be released. Classic example of retain cycle. Make sure your code does not create such dead locks

  4. Never hold anything strongly by using either strong/retain unless its necessary.

  5. Use instrument to find the memory leaks and reference counts of each objects just to make sure there is no memory leaks in ur app.

Sign up to request clarification or add additional context in comments.

7 Comments

@sandeep-bhandaripp thanks for the info... couple of further questions.. bit of a novice, but what do you mean proper navigation techniques ?. I created a single view app & added various VC and seque between them, is that wrong ? also reading about ARC, I assume the VC would let go, but I'm using swift 3 so how do you deinit a VC ?
@user2596590 : If you are using a push/show segue View Controller gets pushed on to navigation stack. View Controllers pushed to navigation stack will not be release until user pops the ViewController by tapping back button or kills the app. So you should use push if u actually want the VC to persist and allow user to come back to that screen on tapping back. If you push loginVC then loginVC will also be persisted, but login is one time used and u don't want user to come back to that screen by tapping back in such cases using push will make LoginVC kept in memory
So any VC which you don't want it to persist u use modal segue and dismiss it to get rid of it once used this will not make the VC being kept in memory. Similarly every time user taps on back button on a pushed VC and pops the VC from navigation stack the VC's deist/dealloc gets called. Make sure it gets called. If its not called that means your VC is being held by something which is not allowing it to be released. Hence memory will not come down.
@sandeep-bhandaripp very useful, Im using modal seque, but I don't always want to go back to the VC. For example VC A -> VC B ->VC C, so if I'm in VC B and use dismiss it would take me back to VC A, but I want to go to VC -C and dismiss VC-B ?
on one of my controllers I do return to the calling VC and have dismiss... and I have put deinit { print("deinit called")} and it does get called , but the memory doesn't go back down ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.