So, I don't really know how to completely delete something from ram. I have been using queue_free() for everything so far, but it is clear that sooner or later, if I don't figure out how to actually delete something, it will cause major problems in the future.
Why isn't queue_free() completely removing the node from ram?
queue_free removes the node from the scene tree. This should remove it from memory eventually unless you still have a reference to that node elsewhere, like in a list: that would prevent the garbage collector from deleting it.
- Edited
I asked ChatGPT.
Me
In the Godot game engine, do nodes get garbage-collected?
ChatGPT
Each node in Godot keeps track of how many references are pointing to it. When the reference count drops to zero, meaning no other objects are referencing it, the node is automatically freed, and its memory is reclaimed. This process is known as automatic memory management through reference counting.
Me
But I thought that nodes in Godot are not reference-counted.
ChatGPT
Nodes are not reference-counted like objects in some other programming languages. Instead, they are managed using a mechanism called "reference ownership." Nodes can be owned by other nodes, and when a node is no longer owned by any other node (i.e., it is not part of the scene tree or owned by any other objects), it is considered for deallocation by the Godot engine. However, the engine does not use traditional reference counting for this purpose.
- Edited
DaveTheCoder LOL Almost everything it "said" is not true.
A node is not memory managed in any way except for deletion when its parent is deleted. You can easily cause memory leaks with nodes. Here's a quick example to try:
extends Node
func _process(delta):
for i in 100:
Node.new()
And here's the result:
I could try this myself to get the answer, but what happens if you create a lot of detached nodes like that, and then stop creating them? Will they persist?
DaveTheCoder Why would engine care to hunt them down only when you stop creating them? Of course they will persist.
- Edited
DaveTheCoder There you go:
extends Node
var itotal = 0
func _process(delta):
if itotal < 1000000:
for i in 1000:
Node.new()
itotal += 1000
- Edited
xyz Almost everything it "said" is not true.
It was paraphrasing the documentation. Maybe that's the problem.
- Edited
Anyway to answer the OP directly @Sabir - queue_free()
will purge the node and all of its children from memory as soon as the current frame processing is finished. free()
will do it immediately. In fact queue_free()
is just a deferred call to free()
. And that call will vanish the object from ram 100% guaranteed. After that, all references to the node still held in your code will become invalid and using them will throw an exception.
If you think something is not getting deleted while it should, use Godot's profiler to check if there are indeed some leaks. But you can rest assured that any leaks you may have are not caused by queue_free()
somehow refusing to delete the nodes.
xyz DaveTheCoder LOL
Almost everything it "said" is not true.
Never forget that a whole new generation of developers is getting raised by ChatGPT and will use it every opportunity they get which isn't a scary prospect at all.
- Edited
Toxe I tend to rant about it from time to time but I get a lot of backlash, typically from people who have no clue about how LLMs operate. For reasons unbeknownst to me they fiercely defend the bot using all sorts of mental gymnastics. But seeing the severity of GPT's rambling, as shown in Dave's interaction, I think I better shut up. As a famous eastern philosopher used to say - never interfere when your enemy is busy making mistakes.