Is clearing an array explicitly by setting values to nil by setting values to nil better letting the GC handling it?

Basically,
Assuming that I have an array t, filled with garbage I don’t use anymore,

First method:

t = nil

Second method:

t = {}
t = nil

Third method:

for k in next, t do
  t[k] = nil
end

Fourth method: (Only applicable when the table is created in a scope)

do
  --create t here
  code(t) -- do stuff with t
end

Except for the fact that the third method takes a bit longer and cannot utilize more thread (if luau use another thread for GC, that is), what are the differences between them?

Try using the table.remove like this:

local t = {}

for i = 1,#t do
table.remove(t,i)
end

Hi!

Tables are just like functions and instances subject to garbage collection. Once they are in so called unreachable state in memory, they are sort of marked as free to be garbage collected. Garbage collector algorithm detects it being no longer needed.

Firstly, suppose we have a simple variable:

local str = "This is a string" -- Allocate a string.
str = "This is another string" -- Allocate another string.
-- The old string is put in unreachable state and is subject to deletion.

Unless we are referencing our variables anywhere else in the code, meaning you are strongly referencing them, they will get cleared. For instance:

local a = "String"
local function printStrings() print(a) end
a = nil -- Most likely won't get "GCed".

What about your four methods?

  1. Method one - setting to nil

As previously said, unless you are not referencing that table elsewhere, it will to my knowledge get garbage collected along all its content of course.

local t = {}; t = nil -- collected
  1. Method two - defining new table and setting it to nil

This method is somewhat unnecessary, as we are allocating a fresh, new table in memory, meaning the old one will (unless referenced elsewhere), switch to unreachable state, a new one will be difined, and after that, the new one will be set to unreachable state again.

t = {"apple", "bannana", "grapefruit"}
t = {}; t = nil
-- Two unique tables get collected.
  1. Method three - clearing each key

This method should clear all keys in table, but not the table itself. It is equivalent to table.remove @Kriko_YT suggested.

local t = {"Ferrari", "BMW", "Honda"}
t[2] = nil -- Seems to be the fastest way!
table.remove(t, 2)
table.remove(t, table.find("BMW"))

table.remove does the same exact thing, but doesn’t work with dictionaries, and is part of lua table library. It’s really fast, but direct removal is faster on micro-level.

None of these, however, schedule table t for deletion.

  1. Method four - wrapping in do blocks

By limiting the scope of specific table, we are only defining them inside those blocks. Once the block has executed, the table will become unreachable in memory, and thus subject to be cleared.

If you are interested in memory leaks and wish to learn more about weak and strong reference of keys, take a look at this post:

2 Likes