簡體   English   中英

為什么我的iOS應用占用大量內存和CPU?

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

我有一個具有簡單菜單屏幕的簡單應用程序。 但是由於某種原因,內存超過130 mb,CPU總是上升到80%以上。 這正常嗎? 還是我做錯了什么?

這是配置文件圖像:

在此處輸入圖片說明

這是菜單場景:

在此處輸入圖片說明

這是調試導航器:

在此處輸入圖片說明

這是代碼:

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.5 mb。 為什么會這樣呢?

您是否正在實際設備上進行檢查? Xcode模擬器使用的內存大約是實際設備的3倍,CPU使用率始終很高。

在實際設備上運行時,您會看到CPU使用率下降很多,內存將下降到40-50Mb。 對於spriteKit游戲來說這是正常的,您不必擔心。

這實際上很難回答,您在做什么是一個好方法,我的意思是使用儀器工具進行分析。 這只是我的2美分,根本原因可能是動畫。 如果執行動畫但沒有正確停止動畫,則它們仍在運行並占用您的內存。 在定制由動畫組成的表格視圖單元時,我遇到了這種情況。 在釋放單元之前,我沒有停止動畫,因此它仍然存在並且正在消耗內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM