- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
cocos2d::CCDirector::sharedDirector()->purgeCachedData();
}
当内存不够时,上述函数会被调用。
void CCDirector::purgeCachedData(void)
{
CCLabelBMFont::purgeCachedData();
CCTextureCache::sharedTextureCache()->removeUnusedTextures();
}
继续追踪,removeUnusedTextures():
void CCTextureCache::removeUnusedTextures()
{
std::vector<std::string> keys = m_pTextures->allKeys();
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); ++it)
{
CCTexture2D *value = m_pTextures->objectForKey(*it);
if (value->retainCount() == 1)
{
CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", (*it).c_str());
m_pTextures->removeObjectForKey(*it);
}
}
}
会将引用计数为1的texture释放。引用计数时ios对内存管理的方式之一。