Unexplainable 16MB malloc in cocos2d app?
I am developing a cocos2d based app and I am now testing it against the Allocations Tool to check whether some leakage is present. I have noticed there are instants at which a malloc of 16 MB occurs and I can actually not say why. I guess it is something related to the cocos framework but I am not sure of it.
Have you ever experienced the same behaviour? Sometimes the app crashes because开发者_运维知识库 of this during the tests.
Thank you in advance
Andrea
Some Futher Details
It seems the problem is related to the way cocos caches sprites. I am using zwoptex texture atlas. Here is the call stack
+[CCSpriteSheet spriteSheetWithFile:capacity:] CCSpriteSheet.m:69
-[CCSpriteSheet initWithFile:capacity:] CCSpriteSheet.m:104
-[CCTextureCache addImage:] CCTextureCache.m:243
-[CCTexture2D(Image) initWithImage:] CCTexture2D.m:207
-[CCTexture2D(Image) initPremultipliedATextureWithImage:pixelsWide:pixelsHigh:]
In that point of the execution I am actually loading the largest sprite I have. It is about 900KB. While the others are very little . Am I using cocos in the wrong way?. For example are there parameters aimed at reducing such peaks. However I would expect that once cached the image would not be loaded any time I need it. Instead I experience new spikes any time I do not use such sprite for some time. I then guessed the cache would not be cleared if I reteined the image but despite of the fact I retain it the malloc still occurs.
I would also like to know why the application RAM footprint changes from 3MB to 80MB when running on the simulator instead of the device
I had experienced the same when I had using the iSimulate(to register accelerometer data and multi-touch(>2) and transfer to the iSimulator).
Ok I guess I can answer my question
I found this method in the app delegate automatically generated when a new cocos2d project is created
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[CCDirector sharedDirector] purgeCachedData];
}
It seems therefore that as soon as the app receives a memory warning the cache is purged. So When I instantiate my 900KB sprite the first time 16MB are allocated (still very strange indeed) and when some times during the execution a memory warning is thrown the cache is purged and it has to load it again. Does it make any sense to you?
Anyway I have commented it and the game seems faster, obviously I have to check very well there are no memory leakages but I am actually pretty sure of it
精彩评论