Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What happens to a coroutine when its' coding finishes executing?

Asked by
Uglypoe 557 Donator Moderation Voter
8 years ago

Say I have this coroutine:

coroutine.resume(coroutine.create(function()
    for i=1,10 do
        print("hi")
    end     
end))

(Note, I'm not using that specifically, it's just an example) Once it prints "hi" 10 times, what happens to the coroutine? Does it cease to exist? Does it just sit there, consuming useful Data? It's one of the reasons I'm scared to use them. Thanks!

2 answers

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

Depending on how and where the coroutine is created, the garbage collector may or may not automatically collect it.

Roblox automatically collects garbage at a set interval. For the purposes of simulation, I'm collecting garbage manually (via collectgarbage()) Since collectgarbage is disabled in Roblox Lua, you may use an online sandbox like this one.

  • Coroutines created (but not stored in a variable) are marked for garbage collection.
collectgarbage()
print("Before coroutines:", collectgarbage("count"))

for i = 1, 1000 do
    -- I don't have to resume the coroutine for it to be marked for garbage collection
    coroutine.resume( coroutine.create(function() end) ) 
end

collectgarbage()
print("After coroutines:", collectgarbage("count"))

prints:

Before coroutines:  27.6806640625
After coroutines:   27.458984375
  • Coroutines created and stored in a variable will be marked for garbage collection after it reaches the end of its lifetime (much like any other variable):
collectgarbage()
print("Before coroutines:", collectgarbage("count"))

for i = 1, 1000 do
    local cor = coroutine.create(function() end )
    -- Again, I don't have to run the coroutine for it to be marked for garbage collection
    coroutine.resume(cor)  
end

collectgarbage()
print("After coroutines:", collectgarbage("count")

prints:

Before coroutines:  27.7314453125
After coroutines:   27.509765625
  • Coroutines that are stored in a variable that has not yet reached the end of its lifetime will NOT be marked for garbage collection, regardless of whether or not it has stopped.
collectgarbage()
print("Before coroutines:", collectgarbage("count"))

local t = {}

for i = 1, 1000 do
    table.insert(t, coroutine.create(function() end) )
    coroutine.resume(t[i])
end

collectgarbage()
print("After coroutines:", collectgarbage("count"))

prints:

Before coroutines:  27.7998046875
After coroutines:   856.1953125
0
Exactly what I was looking for, thank you!! Uglypoe 557 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago

Please provide code with your answers. Simply posting an explanation does not help someone new to programming understand how to implement a concept programatically.

it just stops that piece of code when its done. you honestly can run corountines in any part of the script, not just the beggining.

0
But my main question is what happens to the coroutine itself? Does it no longer exist, or does it just sit there as a data drain? Uglypoe 557 — 8y
0
well it can be either but by default it no longer exists without calling it like local t = coroutine.new(...) ... Angels_Develop 52 — 8y

Answer this question