local rs = game:GetService("RunService")
local function wait(x)
x = tonumber(x) or (1/30)
local start = os.clock()
while (os.clock() - start) < x do
rs.Stepped:Wait()
end
end
Second code:
local function wait(x)
local rs = game:GetService("RunService")
x = tonumber(x) or (1/30)
local start = os.clock()
while (os.clock() - start) < x do
rs.Stepped:Wait()
end
end
Will the first one cause memory leaks? Is that because the run service has a strong reference and is not being cleared?
Another:
while (true) do
local a = workspace.A
local pro = workspace.Pro
end
-- When the while loop ends
So my question, is will everything inside the while loop be cleared once it breaks?
First of all, you are overwriting a global named ‘wait’.
Secondly, you should always predefine variables if possible
(local rs = game:GetService("RunService")). Therefore, the first code block is more recommendable.
I do not see how this would cause memory leaks. You are not doing things like this:
-- Lets leak memory shall we
-- Not sure if this works though
local t = {1}
while wait() do
table.insert(t, t)
end
Lua, and Luau have garbage collection which automatically clear out not used things, i would presume this to be a case which happens here. (garbage collection clears the unused variable after exiting the func)
It was just an example where t keeps existing and becomes bigger and bigger. You should basically never have to worry about memory leakage, unless you are writing (intentionally) outrageous code like I did there. Lua and Luau do all of the cleaning for you. If you want to know more about this subject, I would recommend you to look into ‘Lua garbage collection’.
Not true, when working with events using Connect, memory leaks are very prevalent and a lot of people don’t know it’s happening. PSA: Connections can memory leak Instances!