I have a question for the table manipulation experts

nestedtable = { a nested table with more tables inside which hold values etc }

dictionary = {
    a = {any},
    b = nestedtable
}

wait(10)

clearallitemsintables( nestedtable )
print(dictionary)

how would I clear out a previous reference for B

also what would it print?

this is regarding memory leaks

1 Like

To clear a table and everything in it, just set it to {}. If that’s not what you mean, could you provide more details?

1 Like

When you set it to {}
you are creating a new table

meanwhile the previous table that was being held by the same variable still exists in the memory except it doesn’t have a reference.

if it holds references to things or any bits of information then they will stay in the table until the table is cleared.

1 Like

In a dictionary you can clear out a key,value pair by setting the key to nil.

So you can remove the reference to the nested table “b” by setting dictionary.b = nil.

If you then looped through dictionary, b wouldn’t show up.

If when you set b to nil, b was the last reference to it, then the table will be cleaned up. If you still have a reference (nested table would be a reference) then the data will stick around, just no longer accessible from the dictionary. Otherwise if b was the last reference the data would get cleaned up.

So at the end of your code when you set nested table to a new table, the original reference remains in dictionary.b making that the only reference. So it’s still accessible to dictionary but no longer to accessible to nestedTable.

1 Like

You can just go through the table and search up for it and after that remove it!

for i,Table in (tablewhereyousearch) do
    if Https:JSONEncode(Table) ==  Https:JSONEncode(tableThatYouWantDelete) then
        table.remove(tablewhereyousearch,i)
    end
end
1 Like

Ok. My first attempt was hard to read, so replying to this will hopefully be better.

To start, your code is creating a reference nestedTable to a table.

That means the table now has 1 reference

Then you set dictionary.b to the table as well. Now the table has 2 references.

Then you change what data nestedTable is pointing to. It now points to a new table removing the reference to the old table. So the original table now has 1 reference (dictionary.b). Since there is still a reference the data will not be cleaned up. To remove the reference from dictionary, you would set dictionary.b to something else (or nil to remove dictionary.b entirely)

1 Like

It creates an empty table, meaning the variable has lost everything it had stored previously. The reason you set it to an empty table is because you can’t set it to nil. It can’t exist in memory without a reference. Garbage collection cleans everything that is nil and/or without reference.

This is incorrect in my experience.

Table = { [workspace.Chair] = { Price = 250, Name = “Happy” } }

if I do

Table = {}

then this { workspace.Chair = { Price = 250, Name = “Happy” } } continues to exist meanwhile a new table was created for Table, I didn’t set the previous table to empty, I created a new table and set it to the “Table” variable which now refers to a new table rather than the previous one.

The way I would do it is.

Table = DeepClean(Table) – which goes through the entire table and removes all the keys and essentially makes the table empty.

Once the table is empty then it can be garbage collected.

What makes you think it still exists in memory? Also the only references you need to worry about cleaning are object references, that way they can be garbage collected when destroyed or parented to nil.

1 Like

image

This.

As soon as I started implementing table.clear on all my tables that were not needed the memory went down.

How is that even being tracked? Garbage collection most definitely be clearing anything that’s no longer referenced.

table.clear sets all keys to ‘nil’, although it doesn’t perform a nested clear.

local function nestedClear(t)
	for i, v in pairs(t) do
		if type(i) == "table" then
			setmetatable(i, nil)
			nestedClear(i)
		end
		if type(v) == "table" then
			setmetatable(v, nil)
			nestedClear(v)
		end
	end
	setmetatable(t, nil)
	table.clear(t)
end
2 Likes