If I would have a dictionary with hundreds of thousands of values, and I would like to “remove” half of them (remove = either completely delete them from the dictionary or just set them to nil), would I clog up memory/cause slight performance issues by just setting them to NIL instead of using table.remove()?
I mean to start off, having a dictionary with loads and loads of values isn’t really the best for performance, and I mean; table.remove() would be better than just setting it to a nil value. The value will still “exist” but won’t be shown.
I believe the garbage collector will take care of freeing the memory when you remove keys from your dictionary. Also, setting the key’s value to nil is the same as deleting it from the dictionary, and table.remove won’t work on a dictionary since it expects a number index to be passed.
It was just hypothetically speaking, I don’t have a dictionary with tens of thousands of values, but it is complicated.
Lets just say that, I have a dictionary which has tens of thousands of indexes which WILL be used at some point; but deleted after a few seconds. So on average I would have about, lets say, 50 used indexes at the same time. But every few seconds a few of them get deleted and replaced with new variables placed on random indexes.
I would have 50 array indexes used at the same time on average, but my question was asked this way, because if I just set them to NIL instead of just removing them, after a few minutes the complete list (10k+) indexes will be set to NIL, which is why I had the concern that it would cause performance issues.
No, you can have over a million keys with no performance issues. Setting half of them to nil will not cause memory issues…
Only objects that are gc able go through the garbage collection (strings, tables, function, user data, tread, tables). The table can still be gced when it has 0 references, and so will it’s gc able values (assuming they have 0 references as well) when the garbage collector runs. Local variables are removed from the stack when the scope ends, so if you have a table as a local variable, it will be gced when the scope ends assuming it has only 1 reference, since when the scope ends, that table will have 0 references due to the variable that held it previously, was removed from the stack.
However Lua / Luau are smart and will handle edge cases to properly gc values when needed.