+5 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)

in Engine by (27,902 points)

1 Answer

+3 votes

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.
by (715 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.

Long time since your last comment. But I'm interested in the game engine where resources are reference counted, but scriptes are garbage collected.
Could you share those engines' names or githubs?
I'm not sure if you still follow this question. I'm looking forward to your reply.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.