Reputation: 3193
My game is consuming memory a lot in my phone, how can I resolve this problem?
I've tried leave my objects = null
in final and call the garbage collector, but not resolved.
The game have to create a new sprite a each one minute.
Upvotes: 0
Views: 822
Reputation: 638
you have to cleanup the previously created sprite or object of cocos2d.
removeChild(sprite1, true);
or
sprite1.cleanup()
after that you can assign null to that object.
You can call the method
CCDirector.sharedDirector().purgeCachedData();
to Removes cached all cocos2d cached data.
Upvotes: 1
Reputation: 1873
You have written your game creating sprite every minute, but i think rather to create new sprite replace the sprite image. if you are not doing any functional position operation on that sprite using below code.
spritetest.setTexture(CCTextureCache.sharedTextureCache().addImage("newimage.png"));
Upvotes: 1
Reputation: 91
What's consuming the bulk of memory in a Sprite is its texture not the object itself and as I can see in the cocos2d source for android they're cached (as IO is slow) by the TextureManager by matching filenames. Thus remember to free textures (mainly the big ones) with TextureManager.removeTexture() if you won't need them for a longer time (or re-loading doesn't hurt).
You should also consider implementing an object pool for these sprites. This way you'd simply reuse unused instances instead of throwing them away just to create a new one the next moment.
In case you didn't know, the Android SDK comes with a tool called allocation tracker (which by now is also integrated in the ADT) to analyze memory usage. The issue might not be where you expect it.
Upvotes: 2