memory management in cocos2d x - le duy vu
DESCRIPTION
TRANSCRIPT
![Page 1: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/1.jpg)
MEMORY MANAGEMENT IN COCOS2D-X
Reference Count and AutoReleasePool
Texture Cache
Optimize memory in game.
![Page 2: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/2.jpg)
REFERENCE COUNT IN COCOS2D-X
Reference Count : is an old approach of memory management in C/C++ projects.
Example :
![Page 3: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/3.jpg)
AUTORELEASEPOOL AutoReleasePool:
CCAutoreleasePool can not be NESTED.
CCAutoreleasePool cannot be used in multi-thread.
![Page 4: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/4.jpg)
USE AUTORELEASE POOL A *a = new A(); a->autorelease() . From here you must not interested
release/delete this object . Autorelease pool will delete/release
object ,but when?
![Page 5: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/5.jpg)
//in init scene m_pNode = CCNode::create();
m_pNode = new CCNode(); m_Node->autorelease();
//1 funciton orther in frame orther
m_pNode->setPosition(ccp(0, 0));
![Page 6: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/6.jpg)
class A { CCNode *m_pNode;
public: // pointer dangling!!
~A() { SAFE_DELETE(m_pNode); }; void setNode(CCNode *pNode) { m_pNode = pNode; };
} What happen if at time call ~A(), pNode
deleted?
![Page 7: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/7.jpg)
class A { CCNode *m_pNode; public:
~A() { SAFE_RELEASE(m_pNode); };// OK now
void setNode(CCNode *pNode) {
m_pNode = pNode; pNode->retain(); };
}
![Page 8: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/8.jpg)
TEXTURE CACHE Textures are cached which can later be
used for drawing.
![Page 9: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/9.jpg)
TEXTTURE CCTextureCache CCSpriteFrameCache
![Page 10: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/10.jpg)
CCSPRITEFRAMECACHE
The CCSpriteFrameCache singleton is a cache of all sprite frames. Using a spritesheet and its associated xml file we can load multiple sprites into the cache. From here we can create CCSprite objects with sprites from the cache.
![Page 11: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/11.jpg)
OPTIMIZE MEMORY IN GAME
Avoid loading PNG/JPG Textures. Load textures from largest to smallest. Use .pvr.czz file format. Use particles.
![Page 12: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/12.jpg)
Avoid loading PNG/JPG Textures :In loading texture size 2M .you have to 4M memory.
![Page 13: Memory management in cocos2d x - Le Duy Vu](https://reader036.vdocuments.us/reader036/viewer/2022082916/54bd182c4a7959f95e8b45be/html5/thumbnails/13.jpg)
Load textures from largest to smallest : If you must load 4 file (4M) and 1
file(16M) If load 4 file(4M) before load file 16 M :
max-memory use : 4 x 4 + 16 x 2 = 48 M.
If load 4 file(4M) after loaded file 16M : max-memory use : 16 + 4 x3 + 4 x 2 = 36 M.