Can things like saving a player to a table and not clearing the table when they leave cause a data leak?
The table i’snt really expanding, though.
it’s like:
local Table = {Ply = Player}
And if that player leaves the game but you don’t clear the table, can it cause a leak?
You should bind Players.PlayerRemoving
and clear the references to the player object when they the game. Most of the time, this involves deleting all references to the table containing the player key, not deleting the player key itself, because the table would also leak if you didn’t remove it.
5 Likes
Is it ok to clear the table?
Table = {}
or just make it nil?
Would that fix the leak?
Clearing the table or setting the player’s value to nil will clear up the memory.
You could be more advanced and use weak tables but it’s not for beginners.
2 Likes
Couldn’t we just make the value weak as so:
setmetatable(t, {__mode = "v"})
1 Like
Not without a script that constantly keeps a reference of every player.
Might be a feature request, but weak tables don’t work with roblox instances
local t = {}
setmetatable(t, {__mode = "k"})
local a = Instance.new("Part")
a.Name = "Weak"
a.Anchored = true
a.Parent = workspace
t[a] = true
a = nil
while wait() do
print(next(t))
end
after a while / immediately it will start outputting nil
Ideally it shouldn’t garbage collect instances if they aren’t parented to nil / destroyed xd
Wait, in this case can this cause a data leak:
Part.Touched:connect(function()
local Table = {"a","b","c"}
end
everytime the part gets touched.
If so, how can I prevent it?
The table is cleared out of memory during garbage collection when the function finishes because it’s local.
1 Like
if you do
game.Debris:AddItem(Obj,10)
and by 10 seconds the Obj no longer exists bc it was deleted, can that cause a leak?
As long as you don’t have any other references to Obj
, it’ll be garbage collected.