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

collectgarbage does not exist

Started by ElvishJerricco, 30 January 2013 - 02:37 PM
ElvishJerricco #1
Posted 30 January 2013 - 03:37 PM
collectgarbage() is an essential part of lua in that it allows for weak tables. Weak tables let us have tables whose values get deleted as all other references to those values disappear. But to make that delete happen, the collectgarbage needs to be run, and _G['collectgarbage'] is nil
tesla1889 #2
Posted 30 January 2013 - 04:49 PM
this isn't a bug. dan200 sandboxed computercraft's lua, and collectgarbage is not included in the computer environment, as it can be used to break free of the sandbox.
this is also why the real io API is replaced with an API written using the fs API. otherwise, anyone could edit any files on a server computer.
ElvishJerricco #3
Posted 30 January 2013 - 05:32 PM
this isn't a bug. dan200 sandboxed computercraft's lua, and collectgarbage is not included in the computer environment, as it can be used to break free of the sandbox.
this is also why the real io API is replaced with an API written using the fs API. otherwise, anyone could edit any files on a server computer.

Ah… OK so what am I to do about my weak tables? =/ Should CC run collectgarbage every time the OS yields?
tesla1889 #4
Posted 30 January 2013 - 05:47 PM
what are you trying to do (i.e. explain it without the weak table)
ElvishJerricco #5
Posted 30 January 2013 - 05:57 PM
what are you trying to do (i.e. explain it without the weak table)

Well regardless of what I'm trying to do, the weak table is the important part. But what I'm trying to do is have background tasks running that communicate with foreground tasks. Problem is, when a foreground task closes, the background task doesn't know that. So it keeps the data and it all continues to run as if the foreground part were still up. Sure, the foreground could be programmed to account for this but that's bad style. What I'd rather do is have the background tasks use weak tables to store their data in, so that when a foreground task closes, the background loses the data for that task.

I mean I can work with it just fine. I just thought it was weird and annoying that we're not allowed to use weak tables.
immibis #6
Posted 30 January 2013 - 06:09 PM
How can't we can use weak tables?
Entries in a weak table will be removed when the garbage collector runs, like they're supposed to.
You just can't make the garbage collector run yourself.

Why can't your foreground task tell the background task that it's closing?
Cloudy #7
Posted 30 January 2013 - 08:47 PM
I'm afraid this isn't going to change. Coupled with the fact that collectgarbage in LuaJ actually runs Java's garbage collector, server lag could happen easy.

while true do collectgarbage() end anyone?
giakomo1201 #8
Posted 31 January 2013 - 04:11 PM
I ahve the same issue, no fixed yet :/
theoriginalbit #9
Posted 31 January 2013 - 04:32 PM
I ahve the same issue, no fixed yet :/
and it never will be fixed. see the reply from Cloudy that is ABOVE yours!
Orwell #10
Posted 31 January 2013 - 06:54 PM
I ahve the same issue, no fixed yet :/
and it never will be fixed. see the reply from Cloudy that is ABOVE yours!
I think he just wants his 3 posts to post a question in 'Ask A Pro' :huh:/>: http://www.computerc...dpost__p__82349
It doesn't seem like he's even trying to be relevant.
theoriginalbit #11
Posted 31 January 2013 - 10:13 PM
I think he just wants his 3 posts to post a question in 'Ask A Pro' :huh:/>: http://www.computerc...dpost__p__82349
It doesn't seem like he's even trying to be relevant.
Yeh I saw that a few minutes after I posted this :P/>
NeverCast #12
Posted 01 February 2013 - 09:26 AM
I think he just wants his 3 posts to post a question in 'Ask A Pro' :huh:/>: http://www.computerc...dpost__p__82349
It doesn't seem like he's even trying to be relevant.

Some people make me weep :(/>
And then I want to slap them!

Also, regarding this question – Couldn't you store a boolean someplace that specifies if Foreground still exists?
I too wish we had all the modules of Lua at our disposal sometimes. But it would not be very good for performance or security. I mean I personally would love the debug api :P/>
I'd pull apart every single program with it. call getLocal on all the things!
immibis #13
Posted 02 February 2013 - 06:45 PM
Also, regarding this question – Couldn't you store a boolean someplace that specifies if Foreground still exists?
I too wish we had all the modules of Lua at our disposal sometimes. But it would not be very good for performance or security. I mean I personally would love the debug api :P/>
I'd pull apart every single program with it. call getLocal on all the things!
What do you mean, if Foreground still exists?
We could have the debug API if LuaJ supported more than one Lua state per process.
ElvishJerricco #14
Posted 10 February 2013 - 11:09 AM
I think he just wants his 3 posts to post a question in 'Ask A Pro' :huh:/>: http://www.computerc...dpost__p__82349
It doesn't seem like he's even trying to be relevant.

Some people make me weep :(/>
And then I want to slap them!

Also, regarding this question – Couldn't you store a boolean someplace that specifies if Foreground still exists?
I too wish we had all the modules of Lua at our disposal sometimes. But it would not be very good for performance or security. I mean I personally would love the debug api :P/>
I'd pull apart every single program with it. call getLocal on all the things!

Apologies for not realizing this thread was still getting attention. The problem is that I don't want to have to notify the background task that foreground task has closed. This way if the program is terminated via ctrl+t it still disconnects correctly from the background task. And of course I could just write the program to use only os.pullEventRaw but that's just ugly.

Anyways I've redesigned the system entirely so this isn't an issue for me anymore. It works more like CraftOS in that it just launches an event and expects the foreground to handle that. But still, weak tables would be nice =P
Cloudy #15
Posted 10 February 2013 - 12:36 PM
Weak tables DO exist. You just can't force the garbage collector.
ElvishJerricco #16
Posted 10 February 2013 - 03:04 PM
Weak tables DO exist. You just can't force the garbage collector.

Yes I know but you can't hardly use them without the garbage collector. Not saying you should let us have access to that (that would be bad). Just pointing out that the ability to use weak tables as intended would be nice.
immibis #17
Posted 11 February 2013 - 11:53 AM
Weak tables DO exist. You just can't force the garbage collector.

Yes I know but you can't hardly use them without the garbage collector. Not saying you should let us have access to that (that would be bad). Just pointing out that the ability to use weak tables as intended would be nice.

Intended use of weak tables:

local function actuallyDoExpensiveCalculation(object)
  -- stuff here
end

local cache = setmetatable({}, {__mode="k"})

local function doExpensiveCalculation(object)
  if not cache[object] then
    cache[object] = actuallyDoExpensiveCalculation(object)
  end
  return cache[object]
end