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?