简体   繁体   English

iOS内存使用量变量?

[英]iOS memory usage variable?

I am developing an iPhone game with cocos2d-iphone. 我正在用cocos2d-iphone开发iPhone游戏。

I am particularly interested in how much memory is CCSpriteFrameCache "holding" at the moment. 我对CCSpriteFrameCache目前“拥有”多少内存特别感兴趣。 I am wondering - is there a way to know that? 我想知道-有办法知道吗? Without using any Xcode tools? 不使用任何Xcode工具?

Perhaps there is a variable that will already let me know an estimate memory consumption value on my app? 也许有一个变量已经让我知道我的应用程序上的内存消耗估计值?

Generally speaking the problem you are posing is not easy to solve. 一般来说,您所构成的问题不容易解决。

In the case of CCSpriteFrameCache , since this class contains a pointer to an NSMutableDictionary of sprite frames, which are textures, you could iterate the dictionary and accumulating the texture dimensions (multiplied by the size of each pixel). 对于CCSpriteFrameCache ,由于此类包含一个指向NSMutableDictionary的sprite框架(即纹理)的指针,因此您可以迭代字典并累积纹理尺寸(乘以每个像素的大小)。

Another approach would be converting the dictionary into NSData like this: 另一种方法是将字典转换为NSData,如下所示:

NSData * data = [NSPropertyListSerialization dataFromPropertyList:spriteFrameDictionary
    format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];    
NSLog(@"size: %d", [data length]);

but this would require you to implement the NSCoding protocol for the CCSpriteFrame class. 但这需要您为CCSpriteFrame类实现NSCoding协议。

About accumulating the textures size, you can multiply width by height by pixel size; 关于累积纹理大小,您可以将宽度乘以高度乘以像素大小; the pixel size depends on the pixel format: RGBA8888 is 32 bits, RGB565 is 16 bits; 像素大小取决于像素格式:RGBA8888是32位,RGB565是16位; also you have to take into account that open gl textures have only sizes that are power of 2: 256x256, 512x512, 1024x512 etc. 此外,您还必须考虑到开放gl纹理的大小只有2的幂:256x256、512x512、1024x512等。

Actually if you are concerned about memory consumption of your textures, they are stored by CCTextureCache. 实际上,如果您担心纹理的内存消耗,它们将由CCTextureCache存储。 There is a CCTextureCache (Debug) method called dumpCachedTextureInfo method in there. 那里有一个CCTextureCache(调试)方法,称为dumpCachedTextureInfo方法。 I have not tried it myself, but here : 我自己还没有尝试过,但是在这里:

@implementation CCTextureCache (Debug)

-(void) dumpCachedTextureInfo
{
NSUInteger count = 0;
NSUInteger totalBytes = 0;
for (NSString* texKey in textures_) {
    CCTexture2D* tex = [textures_ objectForKey:texKey];
    NSUInteger bpp = [tex bitsPerPixelForFormat];
    // Each texture takes up width * height * bytesPerPixel bytes.
    NSUInteger bytes = tex.pixelsWide * tex.pixelsHigh * bpp / 8;
    totalBytes += bytes;
    count++;
    CCLOG( @"cocos2d: \"%@\" rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB",
          texKey,
          (long)[tex retainCount],
          (long)tex.name,
          (long)tex.pixelsWide,
          (long)tex.pixelsHigh,
          (long)bpp,
          (long)bytes / 1024 );
}
CCLOG( @"cocos2d: CCTextureCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
}

You want to calculate per texture the bit format, since it is possible to store different texture formats in the cache, depending on your current needs. 您要为每个纹理计算位格式,因为可以根据当前需要在缓存中存储不同的纹理格式。 It will give you (last line) the summary of contents, including total memory consumed. 它将为您(最后一行)提供内容摘要,包括消耗的总内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM