Wednesday, October 12, 2016

Godot (GDScript) Memory Management

I've been working with the cocos2d-x engine for a while and have gotten used to one of the important features in the engine.. the autorelease().

It's basically a reference counting implementation which automatically deletes any object that is removed from a scene. Unless ofcourse you called a retain on it. So when i was working on DotDot a simple 2D game, i created a crosshair animation everytime the user got a perfect alignment and removed the object once the animation was done. It worked great and for a while i assumed that everything was fine.

I was showing off the game to people on my android device when i started noticing slowdowns once in a while.. and as the score got higher it got worse.. surely the engines' fault i assumed.. but then i thought ill' give Godot the benefit of doubt and checked the documentation about freeing the memory. That is when i discovered that the objects removed from the scene are not actually removed from memory. Basically every time i did a perfect align in the game i was leaking memory. I used the inbuilt profiler to try and figure out which object was getting left out in the memory and it was the cross hair object.

So how do we get around this problem?.. Ideally we should be maintaining a pool of the objects and changing position / re animating the object.. but for the game it wasn't that important. I chose the method of creating / destroying the object instead and for this to work i had to use "queue_free()" call in the "_exit_tree()" which is called when the node is removed from tree.

Hope this helps anyone trying to do something similar, this was definitely a big miss and could become a problem if you miss it :)