Do objects get cleaned up automatically after losing reference if the parent is nil?

As title.

Basically something like this:

randomcode()
Instance.new("Part") -- no reference
morecode()

Or more understandably this:

do
  local part = Instance.new("Part")
end

Is this going to be a memory leak?

I tested myself and the results are somewhat unexpected.

wait(10)
print(gcinfo())

do
	local t={}
	for i=1,1e4 do
		t[i] = Instance.new"Part"
	end
	print(gcinfo())
end

while wait(1) do
	print(gcinfo())
end

Output:

  1092
  2058
  1504
  1437
  1522
  1437
  1501
  1588
  1486
  1568
  ...
1 Like

Since the instance hasn’t been parented and there is no reference to it, it is likely it was GC’d automatically.