Is GDScript garbage collected or reference counted?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zylann
:warning: Old Version Published before Godot 3 was released.

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)

:bust_in_silhouette: Reply From: duke_meister

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.

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

Zylann | 2016-05-29 23:55

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.

duke_meister | 2016-05-30 00:27

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 :slight_smile:

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).

Zylann | 2016-05-30 00:34

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

duke_meister | 2016-05-30 00:39

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.

Zylann | 2016-05-30 00:40

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.

vousmevoyez | 2021-04-20 09:27