设备 (iPhone/iPad) 上 Cocos2d/Box2d 应用程序中的内存警告 [英] Memory warnings in Cocos2d/Box2d apps on device (iPhone/iPad)

查看:18
本文介绍了设备 (iPhone/iPad) 上 Cocos2d/Box2d 应用程序中的内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Xcode 中 profile->leaks 的信息,我在 iPad 2 上运行了大约 21 分 12 秒,然后崩溃了.

Here's the info of profile->leaks in Xcode and I ran it on iPad 2 for about 21 mins 12 seconds before crashing.

实时字节 ---- 5.45 MB

live Bytes ---- 5.45 MB

生活----13547

Living ---- 13547

暂态 ---- 3845036

Transitory ---- 3845036

总字节数 -- 720.31 MB

overall Bytes -- 720.31 MB

当应用在设备上运行时,应用在控制台中打印Received Memory Warning时崩溃.

When the app is running on device, the app crashes printing Received Memory Warning in Console.

我不太确定它是如何工作的.

I'm not very sure of how it works.

但是请告诉我,如果某个应用在设备上运行 21 分钟,在此运行期间总共使用了大约 720 MB 的内存,但实时字节从不超过 7.0 MB.

But please tell me if an app runs for 21 minutes on a device, uses an overall of around 720 MB memory in total during this run, yet live bytes never go beyond 7.0 MB.

我接受应用程序使用 3.25 MB 作为活动字节和启动在此运行期间,它的实时字节数达到 5.45 MB &我不太确定活动字节是如何像这样增加的.

但我的问题是:

它是不是一个糟糕到在设备上运行时会产生崩溃的应用程序?

Is it an app bad enough to produce crashes while running on device?

或者

我是否面临其他问题?

推荐答案

你可能会在 CCTextureCache 单例中留下大量的精灵.每次创建 CCSprite 时,都会缓存(静默)纹理,以便下次引用它时,加载和呈现会更快(快得多).在模拟器中运行分配分析(见下面的两张图片):

You probably are leaving tons of sprites in the CCTextureCache singleton. Everytime you create a CCSprite, the texture is cached (silently) so that the next time you refer to it, the loading and presentation will be faster (much faster). Run the allocations profiling in simulator (see two pics below):

顶部图像来自设备上的分配分析.最大内存 4.4 Mb.

The top image is from allocations profiling on device. Max memory 4.4 Mb.

底部图像是相同的应用程序,相同的游戏序列,同时在模拟器中进行分析(峰值约为 78 Mb).通过在模拟器中运行,我可以在分配中看到我的精灵使用的内存.在设备中,分配工具不占用此内存.

The bottom image is the same app, the SAME gameplay sequence, while profiling in the simulator (peaks at around 78 Mb). By running in simulator, i can see in the allocations the memory used by my sprites. In the device, this memory is not accounted for by the allocations tool.

您正在寻找趋势和离散的大跳跃.如果您再也没有回来,您可能会留下未使用的精灵.就我而言,我选择在游戏执行的特定点从纹理中释放特定资源.这是来自 appController 的示例

You are looking for trends and discrete big jumps. If you never come back down, you are probably leaving behind unused sprites. In my case, i choose to free specific resources from the textures at specific point in the game's execution. Here is an example from the appController

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    MPLOGERROR(@"Before purge");
    [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];

    [CCAnimationCache purgeSharedAnimationCache];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [[CCDirector sharedDirector] purgeCachedData];

    MPLOGERROR(@"%After purge");
    [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];

}  

这是最后一搏,蛮力清理.但是您可以在游戏过程中移除不同时间点的特定纹理,而不会影响应用程序的感知"响应能力.缓存在原则上通常是合理的,但在资源有限的情况下可能会迅速变得棘手.了解它并进行试验,最终您会找到什么保持/什么继续"的正确组合,以实现流畅的应用程序性能.

This is last ditch, brute force cleanout. But you can remove specific textures at different point during game play without impacting the 'perceived' responsiveness of the app. Caches are generally sound in principle, but can become rapidly tricky in the face of constrained resources. Learn about it, experiment, and eventually you will find the right mix of 'what stays/what goes' for smooth application performance.

ps.尽管模拟器对于某些测试很好,但不要将其性能"用作基准.模拟器性能在图形方面毫无意义,它不使用您计算机的 GPU(这就是您看到图形内存分配的原因:)).

ps. although the simulator is fine for certain tests, dont use its 'performance' as a benchmark. Simulator performance is meaningless when it comes to graphics, it does not use your computer's GPU (and that is why you see the graphic memory allocation :) ).

这篇关于设备 (iPhone/iPad) 上 Cocos2d/Box2d 应用程序中的内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆