• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] How good is the garbage collector?

Status
Not open for further replies.
Level 24
Joined
Jun 26, 2020
Messages
1,853
Hello, I ask this question because I wanna know how viable is create a vector "class" with Lua, because taking advantage of the garbage collector will make easier do geometry.
I read threads about this but I didn't get a clear answer.
I know there exists [Lua] - Leak Preventer, but tables are easier to work than the locations, or maybe I can do something similar to this like "recycling tables", what can you tell me?
Edit: I discovered [Lua] - Lua wGeometry, but I have other plans.
 
Last edited:

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,890
I believe that the fundamental core of the garbage collector is that once all references (non-local variables, in most cases) to an object (table) are nil, the object will be recycled.
However, there are moments that if you create objects at a constantly, let's say at a periodic timer of 0.03, then the garbage collector won't clean those because you're not letting it a room to breathe. Take this statement with a grain of salt.

 

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,890
Yes, overwriting the previous value isn't a leak.
But, I also have understood that local values are always collected
Depends.
Lua:
do
    local somevalue = "ba"

    function somefunc()
        print(somevalue)
    end
   
    function Test()
        local Testtrig = CreateTrigger()
        TriggerRegisterTimerEventPeriodic(Testtrig, 0.01)
        TriggerAddAction(Testtrig, function()
            local t = {}
            for i = 0, 1000 do t[i] = {} end
        end)
    end
end
The variable somevalue will never be collected, because there's still a location where it is used.
Test function generates 1000 tables very fast non-stop, the garbage collector won't catch up to collect them even if they're local.
Also, if an object is referenced in more than 1 variable, simplying nilling 1 of them doesn't collect the object, it's just removing a reference.
Lastly, there's also other things at hand like weak keys and values, but that I barely know.
 
Last edited:
Status
Not open for further replies.
Top