Garbage collection and memory management of godot objects c#.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Svampen

I’m trying to learn C# by using godot and i’m trying to write a 2D fov algorithm.

However i noticed that the static memory usage and amount of objects in memory skyrockets using the debugger monitor so i wrote a simple memory test to try to figure out how it works.

Example:
private void MemoryTest() { Polygon2D polygon = new Polygon2D(); }

Always results in a new Polygon2D object without getting rid of the reference to the old one so the amount of objects in memory constantly increase.

When using a native c# object such as a List the automatic garbage collection takes care of it and the memory usage remains the same.

I’m trying to get rid of the godot object from memory by using the method Dispose() but it doesn’t work.

What am i missing?

Thanks!

:bust_in_silhouette: Reply From: Zylann

Even if you use C#, you are still interfacing with Godot. Polygon2Dis a Node. Nodes are not reference counted in the engine, and their memory management model goes through parent-child ownership (parent deletes children when deleted).
So they need to be freed explicitely, using queue_free (if they are in the main tree) or free otherwise to make it take effect immediately.