user3904534
user3904534

Reputation: 319

Why is my iOS App taking so much memory and CPU?

I have this simple app that has a simple menu screen. But for some reason the memory is over 130 mb and CPU always rises above 80 percent. Is this normal? or am I doing something wrong?

Here is the profiling image:

enter image description here

Here is the menu scene:

enter image description here

Here is the debug navigator:

enter image description here

Here is the code:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

  var gameScene: SKScene!
  var skView: SKView!

  override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.changeScene(_:)), name: "ChangedScene", object: nil)
    skView = self.view as! SKView
    gameScene = IntroScene(size: skView.bounds.size)
    gameScene.scaleMode = .AspectFill
    skView.presentScene(gameScene)
  }

  func changeScene(notification: NSNotification) {

    let message = notification.userInfo!["sceneName"] as! String

    let transition = SKTransition.revealWithDirection(.Left, duration: 1.0)
    if message == "SelectScene" {
      gameScene = SelectScene(size: skView.bounds.size)
      skView.presentScene(gameScene, transition: transition)
    }

    if message == "MatchingGameScene" {
      gameScene = MatchingGameScene(size: skView.bounds.size)
      skView.presentScene(gameScene, transition: transition)
    }

    if message == "SoundGameScene" {
      gameScene = SoundGameScene(size: skView.bounds.size)
      skView.presentScene(gameScene, transition: transition)
    }
  }

  override func shouldAutorotate() -> Bool {
    return true
  }

  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
      return .AllButUpsideDown
    } else {
      return .All
    }
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
  }

  override func prefersStatusBarHidden() -> Bool {
    return true
  }
}

As you can see the memory usage is 130mb and CPU is over 80 percent. Is this normal? I was expecting it to be much smaller than 130mb and 80 percent because the entire app file including images is just a bit over 2.5 mb. Why is this happening?

Upvotes: 2

Views: 2223

Answers (2)

crashoverride777
crashoverride777

Reputation: 10674

Are you actually checking on a real device?. The Xcode simulator uses about 3 times as much ram as real device and CPU usage is always very high.

Running on a real device you will see that your CPU usage will go down a lot and memory will go down to about 40-50Mb. That is normal for a spriteKit game and you have nothing to worry about.

Upvotes: 2

HSG
HSG

Reputation: 1224

This is actually hard to answer and what are you doing is a good approach, I mean using instrument tool to analyze. This is just my 2 cents that the root cause probably is about animations. If you perform animations but don't stop them properly, they are still running and consuming your memory. I experienced this when customizing a table view cell consists an animation. I did not stop the animation before the cell deallocated, so it was still there and consuming memory.

Upvotes: 1

Related Questions