Hi, I've been having issues with memory leaks in my program. I have boiled it down to this simple reproduction script:
extends Node
class StringContainer extends Reference:
var string: String = "";
func test_memory_leak():
var cs = [];
for i in range(1024):
for q in range(256):
var nm := str(i).repeat(64) + str(q);
var c = StringContainer.new();
c.string = nm;
cs.append(c);
yield(get_tree(), "idle_frame");
cs.clear();
print("Cleared cs");
func _ready():
test_memory_leak();
A bunch of StringContainer objects get added to the array cs
, then cs
gets cleared. Since StringContainer extends Reference, clearing cs
should dereference all of the StringContainers, freeing the memory used.
The in-engine memory monitor suggests that this is happening correctly:

However, the memory monitor suggests otherwise; the memory use stays at around 580.2MiB at the end and never drops.

This doesn't happen if I replace cs.append(c);
with cs.append(nm);
, the memory use grows and then drops back to <60MiB as expected.
How can I avoid this memory leak while storing the string within a class?