Strings don't get garbage collected?

local t=setmetatable({game:GetService'HttpService':GenerateGUID()..'123456789',newproxy()},{__mode='v'})
while wait()do
	print(t[1]~=nil,t[2]~=nil)
end

If you run this code the output [after some time] will be true,false
This means that the userdata is garbage collected but this string which has no other references(?) isn’t?

Strings aren’t Objects, so they don’t get garbage collected. Literals like numbers, booleans, and strings aren’t passed around by reference so it makes no sense for them to be.

strings are passed around by reference, only one copy of each distinct string is actually stored (string interning/string pooling)
thats why == operator works

From Programming in Lua Chapter 17:

Strings present a subtlety here: Although strings are collectible, from an implementation point of view, they are not like other collectible objects. Other objects, such as tables and functions, are created explicitly. For instance, whenever Lua evaluates {} , it creates a new table. Whenever it evaluates function () ... end , it creates a new function (a closure, actually). However, does Lua create a new string when it evaluates "a".."b" ? What if there is already a string "ab" in the system? Does Lua create a new one? Can the compiler create that string before running the program? It does not matter: These are implementation details. Thus, from the programmer’s point of view, strings are values, not objects. Therefore, like a number or a boolean, a string is not removed from weak tables (unless its associated value is collected).

4 Likes