1

I have a Swift 2.3 app being used by 20 people via TestFlight. It's a companion app to another popular app, so users often switch between the two. They use the main app for a bit, then switch over to my app.

One issue that people complain about is that when they switch to my app and go to switch BACK to the main app, the main app has to do a full restart, which can take a few minutes. They claim other apps do not cause the main app to shut down like this and want me to "fix" my app so it doesn't force a restart of the other app.

I'm new to iOS development so this is a bit baffling. Could there be some aspect of my app that would force other apps to terminate? Too much battery draw? Something else? Is there a way to test for this or a metric to aim for to reduce the likelihood of this occuring?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
bflora2
  • 733
  • 3
  • 8
  • 26

2 Answers2

2

Too much memory. That's the primary reason. Your app may be consuming too much memory so iOS has to kill any other suspended apps forcing any other to be started fresh when the user tries to return to it.

Use Instruments and profile your app to find and fix any memory issues.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thats right, and depending on which iphone model this is much early when they did not have enough ram (the current ram is hidden the apple webside iphone specs) but see here [iPhone6](https://en.wikipedia.org/wiki/IPhone_6) and [iPhone6S](https://en.wikipedia.org/wiki/IPhone_6S). and on the 6+ Version because of bigger screen i got always the restart of background apps :( i am now happy with 6S+ because this is gone – muescha Jan 26 '17 at 00:27
2

This is probably a memory leak.

Add this inside AppDelegate:

func applicationDidReceiveMemoryWarning(application: UIApplication) {
    //Print and send to analytics
}

Check this question: How to implement didReceiveMemoryWarning in Swift?

Then add this to all your View Controllers:

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    //Print and send to analytics
    //Dispose of any resources that can be recreated
}

Check Memory Report:

Sometimes when you do an action, like pressing a certain button, you will see a clear rise on the memory usage and it won't fall down after a few seconds. Thats probably a leak.

Also if click "Profile in Instruments" you'll get a detailed report while you use the app about which process is taking which amount of memory.

enter image description here

In my experience memory leaks happen mostly because of closures:

    //Leaks memory
    ez.runThisAfterDelay(seconds: 2) {
        self.doSomething()
    }

    //Doesn't leak memory
    ez.runThisAfterDelay(seconds: 2) { [weak self] () -> () in
        self?.doSomething()
    }

Some sources about closure memory leaks:

Shall we always use [unowned self] inside closure in Swift

http://blog.stablekernel.com/how-to-prevent-memory-leaks-in-swift-closures

Community
  • 1
  • 1
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168