Strange memory leak issue when removing elements from a table?

Unsure if this is a bug or not, but I’ve been having a memory leak issue with my script lately, and I’ve managed to reproduce it. For some reason, after clearing a table (or just removing keys in general), it still takes up memory:

local tab = {}

for i=1, 100000 do
	tab[i] = CFrame.new(1,2,3)
end

print(#tab) --100000

-- Results in memory shooting up to extremely high amounts, as expected.

wait(10)

for i=#tab, 1, -1 do
	tab[i] = nil
end

print("Deleted")

-- After this, the memory usage goes down by a significant amount, but is still 4-5x higher than it was originally.

wait(30)

print("Done")

-- Once the script is done running, memory usage goes back to the normal amount

Of course, after the script is done running all references to tab are gone, and the table is deleted from memory, returning lua memory (measured with gcinfo) to its usual ~750kb. I was able to replicate this both in-game and in studio. The table’s length is 0 and pairs/ipairs being used on the table loops through nothing. I’ve also tried clearing the table with table.remove, but I got the same results.

Does anyone know why this takes up memory if everything in the table has been set to nil?

1 Like

If you’re comparing memory usage from before the script runs to after the table has been cleared but the script is still running, that might cause it. Try putting a wait(10) after the local tab = {} line and use that as your baseline.

I dont know if this is an fix but can you try using;
tab = {}

To establish a baseline, I looked at the memory while the script was disabled. During another test, the empty table also did not cause the memory to rise before it got populated with cframes.

I recommend you use

table.remove(table name, the number position of the value in the table)

@Professor_Boxtrot

As stated in my post, I tried using table.remove but I got the same results unfortunately.

Oof, maybe post something in #platform-feedback:studio-bugs but other than that, I have nothing.