• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Need to put table = nil, for optimize or not?

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
first of all i create a new table:
Lua:
local members = {}

after i add some things in this table, and use this table.
after i need to use, it to optimize? or not make sense? (line 21)
Lua:
members = nil

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local members = {}
    local players = Game.getPlayers()

    for k, targetPlayer in ipairs(players) do
        if targetPlayer:getStorageValue(100) > 0 then
            members[#members + 1] = {name = targetPlayer:getName(), stor = targetPlayer:getStorageValue(100)}
        end
    end
 
    for i = 1, #members do
        if members[i].stor == 1 then
            members[i].name:addItem(2160, 1)
        elseif members[i].stor == 2 then
            members[i].name:addItem(2160, 2)
        else
            members[i].name:addItem(2160, 3)
        end
    end
 
    members = nil
    return true
end
 
Solution
Nekiro is mostly correct here.

OP, what you are really asking about is called garbage collection, something Lua handles automatically for you. You can read about it here.

However there is likely no point in time in which the average OT developer will ever need to modify anything to do with the garbage collector. You are doing something that is known as premature optimization. Until such time as you can profile your servers behavior and demonstrate this is actually a problem, you should leave it alone.
Code:
local members = {} <-- Local outside the function so it will stay in memory until you remove
members = {} <-- Global outside the function so it will stay in memory until you remove

function
local guildMembers = {} <-- local inside will remove automatically after function ends
guildMembers = {} <-- Global inside same thing
members[1] = 1
members = nil
end
 
No, in your case this does not change anything, your allocated memory will be freed when function goes out of scope.
 
Nekiro is mostly correct here.

OP, what you are really asking about is called garbage collection, something Lua handles automatically for you. You can read about it here.

However there is likely no point in time in which the average OT developer will ever need to modify anything to do with the garbage collector. You are doing something that is known as premature optimization. Until such time as you can profile your servers behavior and demonstrate this is actually a problem, you should leave it alone.
 
Solution
Back
Top