Garbage Collection in Cocos2d-x

Hi all devs.

can anyone guide me properly to understand
how garbage collection in cocos2d-x works

i am more acquainted with java, where i know java handles automatic memory deallocation and garbage collection

Please be detailed, so that this would help other learners and developers.

Thanks in advance… :smile:

It’s not garbage collected if writing in c++, instead it uses reference counting with retain/release/autorelease. You won’t normally need to worry about this if you always use the standard create() methods with addChild() and hold onto objects inside cocos2d::Vector<> or Map<> as they handle everything for you, for the most part. You can always manage this yourself if you want.

With Lua/JS bindings they are currently investigating how to improve the API and memory management, but I believe that there’s standard garbage collection within the Lua or JS runtime for non-cocos2d objects and retain/release underneath for cocos2d objects using the bindings. Someone else can likely better speak on this.

Thanks for reply @stevetranby

I use c++ only.
however i need a bit descriptive info about it.

Jason @slackmoehrle could you help… ??

I think this is what you are looking for. But to be honest, I still have not understood this until the end.

Thanks @evWin i hadn’t seen the wiki.
It provides some info.

also @stevetranby you were right, as long as we use static constructors i.e. create()
we don’t need to worry about retain/release of the objects.

So, here’s what i draw conclusion from it ->

  1. when we call static constructors i.e.create() function
    it automatically creates and initializes the object , after finding the appropriate texture
    it will autorelease the object.

  2. this is also applicable for functions with name createWithXyz()

What i didn’t understood in the wiki is what are singletons and how should memory management be used for them also
the wiki pointing to
concern the pair of object->retain() and object->release()

Explaining this will solve the issue…
also any more info is always welcome :smile:

About singleton, you can easily search for it using google, i’m a newbie so i can tell you exactly about it.

About garbage collection in cocos2d-x, as i know, the mechanism is simple. There are 3 method you need to know is autorelease(), retain(), release().

With autorelease(), it means the obj has been marked as “need to be monitored until it no longer use”, when it reach no longer use phase, it will be deleted automatically. If you want to know how the mechanism works i think you should read this: (it involves in count the reference the others refer to obj)

For ex1: in method CreateMyLayer():

sprite = Sprite::Create(…); // inside Create method, they call autorelease() intenally
layer1->addChild(sprite); // at this time layer1 refer to a, reference counter for sprite 0 + 1 = 1

when you delete layer1, the counter for sprite will be 1 -1 = 0, then counter reach 0, sprite will be deleted at this time.

Ex2 in method CreateMyLayer():

sprite = Sprite::Create(…); // inside Create method, they call autorelease() intenally
layer1->addChild(sprite); // at this time layer1 refer to a, reference counter for sprite 0 + 1 = 1
layer2->addChild(sprite); at this time layer2 refer to a, reference counter for sprite 1 + 1 = 2

when you delete layer1, the counter for sprite will be 2 - 1 = 1, therefore, sprite still exist

Ex3 in method CreateMyLayer():

sprite = Sprite::Create(…); // inside Create method, they call autorelease() intenally
but no one refer to sprite, so when it reach }, the reference counter is 0, it will be deleted

In this ex, you didn’t use layer1 to addChild the sprite (or didn’t refer to it). So the existence of sprite is limited to the scope of CreateMyLayer() method, it you use sprite outside this scope, your program will crash.

With retain(), release(), it’s similar to new and delete keyword in C++, when you call obj->retain(), it will exist in memory pool as long as it encounter obj->release().

My understanding about how reference counter tracking may be not exactly, but the general garbage collection in cocos2d-x works that way (if i’m not wrong :smile:) . Hope that help!

3 Likes

Excellent @nbtthief

Just what i need to know, the information is clear is precise to the point.
It cleared my idea about the object retain() and release() also about the autorelease()

Thanks :smile:

@nbtthief this is a great explanation.

Singletons restrict the instantiation of a class to a single instance. They can be extremely useful or extremely painful depending upon how many you are using. It is very easy to get caught up in “Singleton Hell” and make every class a Singleton. This would be considered bad practice in software development.

However, we are writing games. Games adhere to a different way of thinking. I know of game companies that write a lot of singletons. I mean a lot. It does ensure that an object only ever gets instantiated once. That is extremely useful.

Check out: http://gameprogrammingpatterns.com
This site is pretty cool and has lots of examples.