cocos2d& TexturePacker:initWithSpriteFrame为一个小文件消耗16 Mb内存 [英] cocos2d & TexturePacker: initWithSpriteFrame consuming 16 Mb of memory for a small file

查看:279
本文介绍了cocos2d& TexturePacker:initWithSpriteFrame为一个小文件消耗16 Mb内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解为什么通过 initWithSpriteFrame 分配5个小 CCSprite 资源在我的内存中消耗16 MB内存应用。这5个小精灵每个只是.png格式的 24Kb ,但随后通过 pvr.ccz TexturePacker 。以下是我的设置:





pvr.czz 文件的整个大小为 2.5 Mb 。 5个小精灵中的每一个都 66x66 px(@ 2x resolution)。为了与这些设置匹配,我在我的cocos2d初始化中调用以下内容,在启动时预先缓存sprite表:

  [CCTexture2D setDefaultAlphaPixelFormat: kCCTexture2DPixelFormat_RGBA8888]; 
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

for(NSString * fnSheet in kGlobalSpriteSheets){
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:fnSheet];
}

然后调用 addOrb 创建资源,它基本上调用 CCSprite initWithSpriteFrame ,如下所示:

   - (id)initWithAsset:(NSString *)assetName {
CCSpriteFrame * frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:assetName];
return [self initWithSpriteFrame:frame];
}



使用分析器,我们可以看到 addOrb 函数占用16Mb内存:





注意调用堆栈显示14个调用 addOrb function,这是真的,但只有总共5个不同的资产,可以通过这个函数加载。



我跳到我的第一件事尽管我尝试预加载它(?),但是Profiler似乎表示PVR纹理没有被分配直到 initWithSpriteFrame 被调用



到我的应用程序启动完成时,我分配的内存在 225Mb 。这是高端,我正在寻找任何方式来减少这种情况。






编辑: precaching, [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo] 给我这个:

  2014-08-21 10:24:29.060 Aftermath [46067:1115151] cocos2d:cc_fps_imagesrc = 5 id = 2 512 x 64 @ 16 bpp => 64 KB 

2014-08-21 10:24:29.060 Aftermath [46067:1115151] cocos2d:CCTextureCache dumpDebugInfo:1个纹理,用于64 KB(0.06 MB)



分配精灵后:

  2014-08-21 10:24:29.060 Aftermath [46067:1115151] cocos2d:cc_fps_imagesrc = 5 id = 2 512 x 64 @ 16 bpp => 64 KB 
2014-08-21 10:24:29.060 Aftermath [46067:1115151] cocos2d:CCTextureCache dumpDebugInfo:1 textures,for 64 KB(0.06 MB)
2014-08-21 10:25: 15.805 Aftermath [46067:1115151] cocos2d:fonts / cinzel_18.pngrc = 33 id = 5 512 x 256 @ 32 bpp => 512 KB
2014-08-21 10:25:15.806 Aftermath [46067:1115151] cocos2d:menu.pvr.cczrc = 7 id = 6 2048 x 2048 @ 32 bpp => 16384 KB
2014-08-21 10:25:15.806 Aftermath [46067:1115151] cocos2d:cc_fps_imagesrc = 5 id = 2 512 x 64 @ 16 bpp => 64 KB
2014-08-21 10:25:15.806 Aftermath [46067:1115151] cocos2d:backgrounds / space.pngrc = 3 id = 4 2048 x 1536 @ 32 bpp => 12288 KB
2014-08-21 10:25:15.806 Aftermath [46067:1115151] cocos2d:CCTextureCache dumpDebugInfo:4 textures,for 29248 KB(28.56 MB)
pre>

所以,看起来预缓存并不是实际加载文件,而是无论如何:跳出的东西是菜单。 pvr.ccz (包含有问题的精灵)使用 16Mb 的内存,尽管 2.5Mb filesize。我想这只是一个必要的邪恶?我知道文件大小!=内存消耗。

解决方案

文件大小不等于纹理内存使用。请考虑以下计算:

 menu.pvr.cczrc = 7 id = 6 2048 x 2048 @ 32 bpp => ; 16384 KB 

很明显,menu.pvr.ccz的大小为2048x2048,使用4个字节(32 bpp)



因此:

  2048 x 2048 x 4 = 16777216字节(16 MB)

您可能想在TexturePacker中验证图像大小。尝试改变大小约束到除2的幂之外的东西,虽然我认为PVR纹理仍然需要POT大小的纹理。尝试没有伤害。


I'm trying to understand why allocating 5 small CCSprite assets via initWithSpriteFrame is consuming 16Mb of memory in my application. These 5 small sprites are each a mere 24Kb in .png format, but then packed into a pvr.ccz file via TexturePacker. Here are my settings:

The entire size of the pvr.czz file is 2.5 Mb. Each of the 5 small sprites are 66x66 px (@2x resolution). To match with these settings, I invoke the following in my cocos2d initialization, precaching the sprite sheets on startup:

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

for(NSString *fnSheet in kGlobalSpriteSheets) {
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:fnSheet];
}

Then I call addOrb to create the assets, which essentially invokes CCSprite initWithSpriteFrame like this:

- (id)initWithAsset:(NSString*)assetName {
  CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:assetName];
  return [self initWithSpriteFrame:frame];
}

Using profiler, we can see that the addOrb function is consuming 16Mb of memory:

note the call stack shows 14 invocations of the addOrb function, which is true, but there are only a total of 5 different assets which may be loaded by this function.

The first thing that jumps out at me is that the Profiler seems to indicate that the PVR texture is not being allocated until initWithSpriteFrame is called, despite my attempts to preload it (?)

By the time my app startup is complete, my allocated memory is at 225Mb. This is on the high end, and I'm looking for any ways to pare this down.


Edit: After the "precaching", [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo] gives me this:

2014-08-21 10:24:29.060 Aftermath[46067:1115151] cocos2d: "cc_fps_images"   rc=5    id=2    512 x 64    @ 16 bpp => 64 KB

2014-08-21 10:24:29.060 Aftermath[46067:1115151] cocos2d: CCTextureCache dumpDebugInfo: 1 textures, for 64 KB (0.06 MB)

And after the sprites are allocated:

2014-08-21 10:24:29.060 Aftermath[46067:1115151] cocos2d: "cc_fps_images"   rc=5    id=2    512 x 64    @ 16 bpp => 64 KB
2014-08-21 10:24:29.060 Aftermath[46067:1115151] cocos2d: CCTextureCache dumpDebugInfo: 1 textures, for 64 KB (0.06 MB)
2014-08-21 10:25:15.805 Aftermath[46067:1115151] cocos2d: "fonts/cinzel_18.png" rc=33   id=5    512 x 256   @ 32 bpp => 512 KB
2014-08-21 10:25:15.806 Aftermath[46067:1115151] cocos2d: "menu.pvr.ccz"    rc=7    id=6    2048 x 2048 @ 32 bpp => 16384 KB
2014-08-21 10:25:15.806 Aftermath[46067:1115151] cocos2d: "cc_fps_images"   rc=5    id=2    512 x 64    @ 16 bpp => 64 KB
2014-08-21 10:25:15.806 Aftermath[46067:1115151] cocos2d: "backgrounds/space.png"   rc=3    id=4    2048 x 1536 @ 32 bpp => 12288 KB
2014-08-21 10:25:15.806 Aftermath[46067:1115151] cocos2d: CCTextureCache dumpDebugInfo: 4 textures, for 29248 KB (28.56 MB)

So, it looks like the precaching isn't actually loading the files, but regardless: the thing that jumps out is that the menu.pvr.ccz (which contains the sprites in question) is consuming 16Mb of memory despite its 2.5Mb filesize. I guess this is just a necessary evil? I am aware that file size != memory consumption.

解决方案

File size does not equal texture memory usage. Consider the calculation here:

"menu.pvr.ccz"    rc=7    id=6    2048 x 2048 @ 32 bpp => 16384 KB

Obviously menu.pvr.ccz has a size of 2048x2048 and uses 4 bytes (32 bpp) per pixel.

So:

2048 x 2048 x 4 = 16777216 Bytes (16 MB)

You may want to verify the image size in TexturePacker. Try changing Size Constraint to something other than Power of 2, though I think PVR textures still require POT sized textures. No harm in trying though.

这篇关于cocos2d& TexturePacker:initWithSpriteFrame为一个小文件消耗16 Mb内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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