After Closing SKScene, Memory Remains High

泪湿孤枕 提交于 2019-12-05 18:20:55

问题


I use a dispatch_once NSObject to create data pointers. So all game asset pointers are made when the main viewcontroller appears. In order to play a game, the user taps a UIButton corresponding to a particular level on a UIViewController. Let me call it LevelSelectionController. When the game is over, the user will tap a label (SKLabel). And all actions and nodes will be removed.

[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];

Moreover, an SKScene subclass for a particular level delegates the task of returning the user to LevelSelectionController to the viewcontroller presenting game SKView as follows.

- (void)closeScene {
    SKView *spriteView = [[SKView alloc] init];
    [spriteView presentScene:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

The only issue that I have is that the memory remains high when the user leaves the game scene (SKScene). The game requires a lot of assets. So when the game starts, the memory usage will jump to 200 MB. When the user returns to the original level selection view controller, the game simulator is still consuming 200 MB according to Activity Monitor. When the user enters a different level, the memory usage will jump by another 10 MB. So how can I release the memory for the last game once the user leaves SKScene?

I'm using ARC. The Xcode version is 5.1. The development target is iOS 7.1.

Thank you for your help.

-- Edit 1 --

I'm silly. I know what the problem is. When I close the scene, I'm creating a new SKView, which I then set it to nil to get of out the current scene. It works. But that should not be the way of doing it. Instead, I need to set the current SKView to a variable before presenting it. When I close the scene, I need to set that variable to nil. Hmm... I wasn't thinking.

-- Edit 2 -- There's little change when the current scene is presented with nil. Removing it from removeFromSuperview doesn't do much.


回答1:


It has been noticed by a few people on SO that an SKScene gets deallocated when the containing SKView is removed from it's superview.

Have a look at these questions and their answers:

Deallocate SKScene after transition to another SKScene in SpriteKit

iOS 7 Sprite Kit freeing up memory

Also, try modifying the closeScene method like so:

- (void)closeScene {
    SKView *spriteView = (SKView*)self.view;
    [spriteView presentScene:nil];
    [self.navigationController popViewControllerAnimated:YES];
}



回答2:


Put NSLog() to SKScene's dealloc method to be sure that it's deallocating.

Also, resources may be not released just after reference count of your scene reaches 0. Due to internal optimizations assets may stay in memory until receiving Memory warning signal.



来源:https://stackoverflow.com/questions/22704219/after-closing-skscene-memory-remains-high

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!