2

The app is a survey in which a user answers a series of questions on different ViewControllers, and the one variable is changed accordingly, presenting a final number at the end of the app.

The code is very basic, and it runs smoothly on the simulator, but crashes when testing on my iPhone due to memory warning.

There are three potential pieces of code using a lot of memory. There are a decent amount of animated images (200 in total) used like so:

cloudAnimation.animationImages = [
        UIImage(named: "Home Page Cloud Animation 01.png")!,
        UIImage(named: "Home Page Cloud Animation 02.png")!,
        UIImage(named: "Home Page Cloud Animation 03.png")! ]

   cloudAnimation.animationDuration = 6
   cloudAnimation.startAnimating()

Each new ViewController adds about 50-60MBs of data to memory, and I think it may be from the segues. There are 2-3 buttons on each screen. Depending on which they click, the variable is changed. Here is that code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yesButton" {
            var DestViewController : YesViewController = segue.destinationViewController as! YesViewController
            DestViewController.percentChance = 0.02
        } else if segue.identifier == "noButton" {
            var DestViewController : NoViewController = segue.destinationViewController as! NoViewController
            DestViewController.percentChance = percentChance
        } else if segue.identifier == "goHome" {
            var DestViewController : HomeScreenViewController = segue.destinationViewController as!
            HomeScreenViewController
        }
    }

The only other thing that could possibly be messing with the memory is that every view holds the "percentChance" variable, and it is changed and passed from one to another.

Do any of these examples look like they could be using large amounts of memory? How can I deactivate or de-initialize ViewControllers or animations once I am done with them? Any help is greatly appreciated.

M. Rudolph
  • 21
  • 3
  • Did you use any of the Instruments to measure your app's memory usage? – Aaron Brager Sep 02 '15 at 01:51
  • You could also try setting your images to `nil` in `viewWillDisappear` and building them in `viewWillAppear` – Aaron Brager Sep 02 '15 at 01:51
  • read : [everything about memory in swift](http://stackoverflow.com/questions/27958012/swift-managing-memory) – R Menke Sep 02 '15 at 03:47
  • The simulator doesn't simulate ram limits of devices. So always test on a device. I also started in the simulator, it's fast and easy and if you have multiple screens, a really nice workflow. But it is just a bad start. I could tell you about one or two ways to reduce memory usage, but it is better to get a full grasp of managing memory. Doing every step right is important here – R Menke Sep 02 '15 at 14:47
  • Thank you all, one more follow up question. How do I dismiss views when I am done with them? – M. Rudolph Sep 03 '15 at 20:21
  • The real key problem is that you cannot hold that many images decoded in memory at the same time. By reworking your approach so that only 1 decoded image is in memory at a time you can avoid these memory problems. – MoDJ Mar 31 '16 at 21:47

0 Answers0