【问题标题】:My app in background is using 70Mb, how to release memory?我的后台应用程序正在使用 70Mb,如何释放内存?
【发布时间】:2014-11-02 07:49:21
【问题描述】:

我有一款游戏在运行时以 60fps 的速度运行,并且在各个方面都或多或少有效率,但问题是当我进入后台时。我通过两种方式暂停,第一种方法在您触摸游戏中的暂停按钮时运行:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        for (UITouch *touch in touches)
        {
            CGPoint location3 = [touch locationInNode:self.pauseLayer];
            SKSpriteNode *touchedNode3 = (SKSpriteNode *)[self.pauseLayer nodeAtPoint:location3];

            if (touchedNode3.physicsBody.categoryBitMask == pausa)
            {
                    if (_gameIsPaused == NO) 
{
                    [self.world setPaused:YES];
                    [self.backWorld setPaused:YES];
                    [self setPaused:YES];
                    _gameIsPaused = YES;
                    [self.pauseLayer setPaused:NO];
                    [self showPauseMenu];
                }
else
                {
                    [self.world setPaused:NO];
                    [self.backWorld setPaused:NO];
                    [self setPaused:NO];
                    _gameIsPaused = NO;
                    [self hidePauseMenu];
                    [self.pauseLayer setPaused:YES];

                }

当你进入后台时,我的应用委托中有这个:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

[[AVAudioSession sharedInstance] setActive:NO error:nil];
SKView* view = [self getSKViewSubview];

    if (view) {
        view.paused = YES;
    }

}

问题是在后台运行时,我的应用程序仍在消耗内存,有时当我打开另一个应用程序时,我的游戏崩溃了。在这张图片中,您可以看到我的应用程序在后台运行时的消耗情况:

我的问题是如果有办法防止崩溃,我应该在离开游戏之前手动释放内存吗? 我尝试过两种方式进入后台,启用暂停菜单和禁用暂停菜单。有时它会崩溃,看起来像是随机的。因此内存消耗总是相同的。最好的做法是释放除点、玩家位置、时间、当前世界等重要内容之外的所有内容并摧毁其他所有内容吗?在这种情况下,我该怎么做?摧毁一切并保持分数,积分等?

【问题讨论】:

  • 你最好的办法是重现和分析当你的应用程序在后台分别从中恢复时发生的崩溃。不过,您不必担心应用程序的后台内存使用情况,iOS 会处理这些问题。我相信它甚至可以拍摄内存快照并将其存储在磁盘上以供以后恢复,而不是将内容保存在“真实”内存中。此外,如果您发现后台模式存在问题,您可以简单地为您的应用关闭后台模式。

标签: ios objective-c sprite-kit


【解决方案1】:

iOS 会在需要更多内存时终止后台应用程序,而不会向应用程序发出任何警告。由应用程序在进入后台时保存其状态以防系统杀死它,然后在启动时再次恢复状态。

如果当系统决定终止您的应用程序时您仍在 Xcode 中调试,如果我没记错的话它会显示为 SIGKILL,但这不是崩溃,iOS 只是在没有警告的情况下终止您的应用程序。

在后台降低内存使用的唯一方法是在应用程序进入后台时卸载图像等,但是对于在用户切换应用程序时可能导致烦人的加载/冻结的游戏......通常所有应用程序都需要要做的是在收到内存不足警告时释放任何不必要的或可重新加载的资源(缓存的图像等)...

【讨论】:

    【解决方案2】:

    这是我最接近 SIGKILL 的东西,当我打开另一个游戏时会发生这种情况,就像行尸走肉一样的 3D 游戏。只发生在大型应用程序上,而不是 whats'app 或 safari,它不会恢复任何状态。

    我得到这个试图重现崩溃以上传图片。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2011-05-13
      • 2019-02-10
      • 2011-05-25
      • 2015-11-06
      • 2017-05-14
      相关资源
      最近更新 更多