gpt4 book ai didi

ios - 我的应用程序在后台使用了 70Mb,如何释放内存?

转载 作者:行者123 更新时间:2023-11-29 02:33:42 29 4
gpt4 key购买 nike

我有一款游戏在运行时以 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];

}

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

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

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

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

最佳答案

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

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

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

关于ios - 我的应用程序在后台使用了 70Mb,如何释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606160/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com