Skip to content

Instantly share code, notes, and snippets.

@outsinre
Created September 21, 2023 07:19
Show Gist options
  • Save outsinre/28cc38d50d6ee8a99e0058e448603326 to your computer and use it in GitHub Desktop.
Save outsinre/28cc38d50d6ee8a99e0058e448603326 to your computer and use it in GitHub Desktop.
table_shrinking.lua
-- memory test
local size = 1000000
local t = {} -- create a table
local function report(title)
collectgarbage()
collectgarbage()
print(title, collectgarbage("count"))
end
report("start")
for i = 1,size do -- fill the table with values, causing the c-size structure of the table to expand several times over
t[i] = {}
end
report("filled")
for i = 1,size do -- clear individual values from the table. But the table structures itself will not shrink back
t[i] = nil
end
report("emptied") -- replace the table structure, such that it get's GC'ed and it finally releases its memory
t = {}
report("cleared")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment