简体   繁体   中英

How can you remove a CCNode from a scene and make sure it is released from memory? Cocos2d V3

I have a scene where I add the node 'page';

[self addChild:_page z:-1]; 

'page' is initialized in the header file as

@property CCNode* page;

When I remove the page from the scene

[self removeChild:_page];

the memory remains the same (as seen through the debug navigator).

This is a problem because I add and remove many pages that contain hd images, animations, and physics environments. I receive low memory warnings after adding and removing several nodes, and the app will crash.

Below is the code that removed most of the content of the CCNode from memory. Note the node did not contain audio.

At the top of your implementation file add the following line.

#import "CCTextureCache.h"

In your implementation file also add an onExit method.

-(void)onExit{
    [super onExit];
    [self removeAllChildren];
    [[CCTextureCache sharedTextureCache]removeUnusedTextures];
}

To make sure that textures were removed you can use

[[CCTextureCache sharedTextureCache]dumpCachedTextureInfo];

Use the debug navigator in Xcode to monitor your memory in real time running an app.

Other research on adding/removing nodes:

In cocos2d V3.x you "don't have to worry" about memory because once your app reaches a certain memory threshold for a device, and you get the 'low memory warning', CCAppDelegate will automatically purge cached data.

// purge memory
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [[CCDirector sharedDirector] purgeCachedData];
}

However, in a case where you load big, sometimes, multiple texture files and there might be a chance of the app exceeding the memory threshold, keeping memory low can be beneficial.

Removing unused data might also help remove code that might have potential memory leaks or bugs that can affect your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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