Do threads take Memory Space?

As the title implies, I want to know if threads take memory space and thus if I should use task.cancel() on threads (which aren’t loops).

tasks should be automatically collected after completion, and every object takes up memory xd

1 Like

So I shouldn’t really care about using them?

1 Like

If you are really concerned about memory leaks, simply cancel them at the end of the script

1 Like

It just loops through a table to damage those characters, afterwards nothing more

then it shouldn’t be heavy, also why do you need thread? you yield something? if not then you can remove it

1 Like

I just put an for i, v in pairs() do since if the table is too big, it can take some time

They will only take up memory as long as they keep working. The garbage collector is automatic, so it will clear up threads that are not running. For example:

task.spawn(function()
    task.wait(1)
    -->> thread gets garbage collected after 1 second because nothing else will happen
end)

task.spawn(function()
    for i, v in ipairs({1, 2, 3}) do
        print(i)
    end
    -->> thread gets garbage collected after printing "1, 2, 3"
end)

local foreverTask = task.spawn(function()
    while task.wait() do
        print("running...")
    end
    -->> thread never gets collected because this will run forever *
end)

task.wait(1)

task.cancel(foreverTask) --> * unless you cancel it, in which it can now be collected
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.