OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么我的 iOS 应用程序占用如此多的内存和 CPU? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:57
标题: ios - 为什么我的 iOS 应用程序占用如此多的内存和 CPU?

我有一个简单的应用程序,它有一个简单的菜单屏幕。但由于某种原因,内存超过 130 mb,而 CPU 总是上升到 80% 以上。这是正常的吗?还是我做错了什么?

这是配置文件图片:

enter image description here

这里是菜单场景:

enter image description here

这里是调试导航器:

enter image description here

代码如下:

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
  }
}

如您所见,内存使用率为 130mb,CPU 超过 80%。这是正常的吗?我预计它会比 130mb 和 80% 小得多,因为包括图像在内的整个应用程序文件仅略高于 2.5mb。为什么会这样?



Best Answer-推荐答案


您实际上是在检查真实设备吗? Xcode 模拟器使用的内存大约是真实设备的 3 倍,并且 CPU 使用率总是很高。

在真实设备上运行你会发现你的 CPU 使用率会下降很多,内存会下降到大约 40-50Mb。这对于 spriteKit 游戏来说是正常的,您无需担心。

关于ios - 为什么我的 iOS 应用程序占用如此多的内存和 CPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36587610/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4