+4 votes

How does GDScript performs garbage collection?
Are objects collected after some time? (this can introduce framerate stalls)
Are objects freed immediately when they loose the last reference? (no gc kicks)

asked May 29, 2016 in Engine by Zylann (16,570 points)

1 Answer

+1 vote

Just a quote from the manual, does this help or did you already see that?

Memory management
If a class inherits from Reference, then instances will be freed when no longer in use. No garbage collector exists, just simple reference counting. By default, all classes that don’t define inheritance extend Reference. If this is not desired, then a class must inherit Object manually and must call instance.free(). To avoid reference cycles that can’t be freed, a weakref function is provided for creating weak references.
answered May 29, 2016 by duke_meister (689 points)

Ok, I didn't see it. So, I assume this is true for GDScript as well.

Not sure what you mean? But yeah it applies to GDscript. Your objects/nodes will be reference counted unless you script specific inheritance from Object, from what I understand. I haven't played with this at all though.

I used game engines where resources were reference counted, but scripts were garbage collected (C#, Lua...). But since GDScript has been written by the same developer for the same engine, I assume it follows the same reference counting scheme. Just to clarify :)

I have .new() calls every frame and in garbage collected languages it would be doomsday. But in reference counted ones, the cost is smoother (I mean, CPU time increase is distributed rather than concentrated in large frame drops).

ah cool. And I guess you'd know about free() vs queue_free() in Godot . And WeakRef

I know about queue_free() for nodes, and I don't use free() because all objects I create are always references, so they free() themselves when going out of scope.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.