Your code isn’t doing what you think it’s doing. In the for loop of your VariableClean function, doing item = nil sets the item variable to nil, but it does not modify the contents of Variables in any way. The same thing goes for the Variables parameter; doing Variables = nil does not actually modify the table that Variables is referencing – it just tells the script that you want the Variables parameter to stop referencing the table. You need to set the keys of the table to nil directly:
function maid.VariableClean(Variables)
for num in pairs(Variables) do -- don't need the item variable
Variables[num] = nil -- remove the table key directly
end
-- no point in doing Variables = nil
end
When the function ends, the Variables parameter ceases to exist thus removing its reference to the table you passed in. Edit: Even if you do Variables = nil in the module script, the Variables reference in the local script still doesn’t get cleaned when you do maid.CleanAll(Variables) (the Variables parameter in the module script is nil but the Variables in the local script is still referencing the same table thus leaking memory anyway). If you’re using a language with pointers like C then you can achieve something like this, but Lua doesn’t give you precise control over memory addresses. You still have to set the Variables in the local script to nil every time you call maid.CleanAll:
-- local script
maid.CleanAll(Variables)
Variables = nil
I wouldn’t recommend doing this anyway. Having to keep all your variables in a table adds extra overhead plus it isn’t necessary for good memory management. There are open-source maid modules that you can use if you want, but I manage 17,000 lines of code just fine without a maid. My servers run up to 24 hours at a time and don’t experience any growth in memory. You just have to watch out for upvalues in event callbacks.