Go to Page... |
Thread Tools | Display Modes |
05-14-06, 10:55 PM | #1 |
Garbage Collection and Local Variables
I don't consider myself an expert at LUA, code, or general coding practice so I lay myself at your mercy to help show me the way. In my experience, garbage collection occurs when local variables (used within a function, class, etc) become no longer 'needed' by the system. As such, it's information is dropped into the garbage bin to be recycled later.
From what I've read, everything in LUA is linked in some way (I think). So a: Code:
variable = nil; Code:
variable2 = nil; Code:
function x() local array = {}; local arraySize = 20; for i = 0, arraySize do array[i] = "This is some data on line " .. i .. " in array{}."; end end How can I minimize the the amount data that is GC'ed from the array, or any variable for that matter, so that the amount of data dumped comes at a minimal cost to the user? Sorry for the scrambled thoughts and thank you for your help. Last edited by chuckg : 05-14-06 at 11:28 PM. |
|
05-15-06, 03:59 AM | #2 | |
Some languages collect immediately when an object's reference count decrements to 0. I believe LUA's GC occurs at some indeterminate point later, however the basic philosophy remains the same. When an object (and *everything* is an object) is no longer referenced by any variables it will (at some point) be collected (excepting interned constants which just hang out forever because they get used so often). In the case of local variables, an automatic reference is bound by the call stack frame. When your function exits, as long as you are not returning a reference to a local, the count is decremented to 0 and thus automagically eligible for collection. Keep in mind that, as a logical requirement of purely referencing languages, all strings are immutable. This means that: Code:
for i = 0, 100 do foo[i] = "Bar_" .. i; end The other side-effect of immutable strings is that: Code:
foo = "bar"; foo = foo .. "baz"; If possible, when designing functions that will return complex/large strings, store data components in arrays until all work is completed and then join the final output (rather than iteratively appending pieces with the ".." operator). |
||
05-15-06, 11:11 AM | #3 | |||||
It's NOT correct, however, to say that Lua is a pure reference language, depending on your data type you may have a value or a reference, though for a given type something is always a value or always a reference (The 'value' types are: nil, boolean, number, light userdata). It's also not true to say it uses reference counting, the garbage collector is a mark-and-sweep model, since reference counting fails to compensate for self-referential-but-otherwise-unreferenced objects.
Code:
function a() local y = 7; return function() return y; end, function(x) y=x; end; end
y = a .. b .. c .. d .. e .. f; To not use intermediates, but instead perform a single concatenation (Provided the resulting string is shorter than 8K or so, otherwise it needs to allocates working buffers) Last edited by Iriel : 05-15-06 at 11:13 AM. Reason: Removed accidental extra quote, fixed negation |
||||||
WoWInterface » Developer Discussions » General Authoring Discussion » Garbage Collection and Local Variables |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|