How to properly clear the memory when switching scene?


#1

I come across to the problem where memory’s growing and growing after switching back and forth between scenes.
Say I have mainmenu and gameplay scene. I tested it by switching back and forth in which each scene has their own things to load. This test leads me to several fixes for all (hopefully) game objects to be released.

Starts from mainmenu, I have 10\ MB\ in\ memory\ usage,\ then\ switched\ to\ gameplay\ the\ memory\ usage\ goes\ to40. Now I switched back to mainmenu, it’s ~20 MB. So there’s around 10 MB incurred to memory usage. I checked everywhere and can’t find a clue of what else needs to be taken care of.

At this point, I would like to ask

  • How to properly clear the memory when switching scene? What’s your practical solution that works in your project, I would like to know that if you want to share.

Thank you!


#2

Probably you have memory leak.

>What’s your practical solution that works in your project, I would like to know that if you want to share
There are CCPointer class on github. Use this class for all strong references, and keep only weak references as raw pointer.

Weak reference == reference to node that already added to scene or to CCDictionary/CCArray and shouldn’t be referenced from any other point (e.g. sprite object that should exists while it is visible on scene).

Do not call retain/release manually, remember that object created with CCObject::copy() or new Class() have +1 reference, object created with Class::create() or Action::reverse() haven’t (since it was autoreleased).


#3

Thanks Sergey, I noted this as an alternative if I cannot solve with my current approach in which I’ve gone too far.

It turns out that ref-count is my biggest issue. Some objects won’t get clearing properly because of they’re retain somewhere either by creating cached actions (this also increase ref-count via retain() call internally), or else. Thus I create another function dealloc() to clear these things out when need with the final purpose for them to call destructor\ function\ after\ that\ when\ ref-count\ reaches\ 0.\ Now\ I\ can\ trim\ down\ leaks\ occurred\ from\ this\ situation\ to\ very\ minimal\ values\ from10 MB each time I switched scene. I’m still finding them where’s left …

It’s so manual process, and like a hardcore memory management :confused:


#4

I checked now that I have no memory leaks via Profiler. But there’s some memory incurred every time I switched back forth the scene.
What’s the problem?


#5

I solved the problem now. The less is get ride by using purging data function from CCDirector class (http://www.cocos2d-x.org/boards/6/topics/4908).


#6

using that is messy,
keeping track of your retain and release is better.


#7

@daniel I always keep track of retain and release. But I’m not sure for some reason, there’s something hidden which I can’t remove those memory out. Profiler shows no leaks, but if there’s any at all, their size can’t contribute to overall incurred memory I see. I only purge cached data when ever I switch scene, no other places.