简体   繁体   English

为什么我的iOS应用占用大量内存和CPU?

[英]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. 但是由于某种原因,内存超过130 mb,CPU总是上升到80%以上。 Is this normal? 这正常吗? or am I doing something wrong? 还是我做错了什么?

Here is the profiling image: 这是配置文件图像:

在此处输入图片说明

Here is the menu scene: 这是菜单场景:

在此处输入图片说明

Here is the debug navigator: 这是调试导航器:

在此处输入图片说明

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. 如您所见,内存使用量为130mb,CPU超过80%。 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. 我期望它比130mb小得多,并且比80%小,因为包括图像在内的整个应用程序文件仅略超过2.5 mb。 Why is this happening? 为什么会这样呢?

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. Xcode模拟器使用的内存大约是实际设备的3倍,CPU使用率始终很高。

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. 在实际设备上运行时,您会看到CPU使用率下降很多,内存将下降到40-50Mb。 That is normal for a spriteKit game and you have nothing to worry about. 对于spriteKit游戏来说这是正常的,您不必担心。

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. 这只是我的2美分,根本原因可能是动画。 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. 在释放单元之前,我没有停止动画,因此它仍然存在并且正在消耗内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM