This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LBPHacker's profile picture

When does garbage collection happen in CC?

Started by LBPHacker, 07 January 2014 - 09:18 AM
LBPHacker #1
Posted 07 January 2014 - 10:18 AM
I just recently discovered weak tables. That brings up a question (see the title of the topic).
Lyqyd #2
Posted 07 January 2014 - 10:45 AM
Whenever it wants to/needs to. I've never noticed any GC issues. I don't know how well LuaJ supports weak tables.
Moody #3
Posted 07 January 2014 - 10:52 AM
As CC uses LuaJ, which runs java 100%, it most likely behaves like the java GC - which is described here: ( http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html )
LBPHacker #4
Posted 07 January 2014 - 05:18 PM
Anyway. I managed to force a garbage collection cycle using this sneaky piece of code (CC 1.53 (yeah, I know, outdated version)):
function collectgarbage()
    local a = setmetatable({{}}, {__mode = "v"})
    while a[1] do
        term.setCursorPos(term.getCursorPos())
        -- Not doing anything in here breaks the top-level shell for some reason
    end
end

collectgarbage()
print("gotcha")
I'm not sure if that really forces a GC cycle, but it certainly prints "gotcha"
Edited on 07 January 2014 - 04:18 PM
wieselkatze #5
Posted 08 January 2014 - 06:42 AM
<p>Found this command in the lua documentation, too.
But it doesn't seem to work: lua-prompt says to
collectgarbage
nil

So there isn't that function from computercraft itself/p>
Edited on 08 January 2014 - 05:45 AM
KaoS #6
Posted 08 January 2014 - 09:28 AM
AFAIK it tosses all data that is not accessible via a handle/variable as soon as the variable is redefined
Moody #7
Posted 08 January 2014 - 11:15 AM
Garbage collector manual calls were removed from cc due to safety and performance. The garbage collection is called as soon as an object is dereferenced (and in between when it expects a object to die, see aging process ). It can take some time if there are many object to collect but usually is instant.