Fortera Global
Intermediate OT User
- Joined
- Nov 20, 2015
- Messages
- 1,180
- Solutions
- 2
- Reaction score
- 117
What can I do to optimize the use of RAM? Script examples that can take up too much RAM?
tfs 1.x+
thanks
tfs 1.x+
thanks
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local player = Player(cid)
if isInArray({"soft", "boots", "repair", "soft boots"}, msg) then
npcHandler:say("Do you want to repair your worn soft boots for 10000 gold coins?", cid)
npcHandler.topic[cid] = 1
elseif msgcontains(msg, 'yes') and npcHandler.topic[cid] == 1 then
...
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local player = Player(cid)
if isInArray({"soft", "boots", "repair", "soft boots"}, msg) then
npcHandler:say("Do you want to repair your worn soft boots for 10000 gold coins?", cid)
npcHandler.topic[cid] = 1
elseif msgcontains(msg, 'yes') and npcHandler.topic[cid] == 1 then
...
npcHandler.topic[12343234] = 1
function onCreatureDisappear(cid)
npcHandler:onCreatureDisappear(cid)
end
function onCreatureDisappear(cid)
npcHandler:onCreatureDisappear(cid)
npcHandler.topic[cid] = nil // = nil deletes value from LUA table
end
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local player = Player(cid)
if isInArray({"soft", "boots", "repair", "soft boots"}, msg) then
npcHandler:say("Do you want to repair your worn soft boots for 10000 gold coins?", cid)
npcHandler.topic[player:getGuid()] = 1
elseif msgcontains(msg, 'yes') and npcHandler.topic[player:getGuid()] == 1 then
...
In 2018:
Get better dedic.
Script that consume too much RAM? Scripts with memory leak.
In many cases it's better to leave small memory leak in LUA to reduce CPU usage.
Example:
Some temporary table with NPC state.
There is table 'npcHandler.topic' indexed by 'cid'. 'cid' changes every relog, so after player logout there stays something like:PHP:local function creatureSayCallback(cid, type, msg) if not npcHandler:isFocused(cid) then return false end local player = Player(cid) if isInArray({"soft", "boots", "repair", "soft boots"}, msg) then npcHandler:say("Do you want to repair your worn soft boots for 10000 gold coins?", cid) npcHandler.topic[cid] = 1 elseif msgcontains(msg, 'yes') and npcHandler.topic[cid] == 1 then ...
There are 2 ways to fix it:PHP:npcHandler.topic[12343234] = 1
1. In onDisappear event of NPC remove key from table.
Change:
To:PHP:function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
PHP:function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) npcHandler.topic[cid] = nil // = nil deletes value from LUA table end
2. Use GUID as table index, not 'cid'. It does not change when player relog, but it will stay in RAM after logout anyway. It will reduce RAM usage to 1 key=value pair per player:
PHP:local function creatureSayCallback(cid, type, msg) if not npcHandler:isFocused(cid) then return false end local player = Player(cid) if isInArray({"soft", "boots", "repair", "soft boots"}, msg) then npcHandler:say("Do you want to repair your worn soft boots for 10000 gold coins?", cid) npcHandler.topic[player:getGuid()] = 1 elseif msgcontains(msg, 'yes') and npcHandler.topic[player:getGuid()] == 1 then ...
Both fixes require some programmer who would analyse every NPC and add changes in proper places. Programmers are expensive, hardware is cheap.
Both fixes will increase CPU usage (call to :getGuid() or remove table value) and reduce RAM usage by few MB per day.
Can you buy faster CPU? Fastest you can get in OVH is 4.7GHz. Soon there will be 5.0GHz, but you can't pay extra 100 euro and get 9.5GHz.
Can you buy more RAM? Of course. 4.7GHz dedic has 64GB RAM. Server will small memory leaks after 24 hours online with 1000 online will use less then 8GB.
In case of serious applications that must run 24/7 without restarts - memory leaks are not acceptable. In case of OTS - it's not a big problem.
After player logout, data connected with player is not removed from memory = memory leaks.In this example you aren't showcasing memory leak but application level cache. *just saying*
As gesior say it's just simple variable that you store in table and it reside in the ram this is because this table is linked with npc so until you remove this npc the garbage collector will not get rid of the data inside the table.In this example you aren't showcasing memory leak but application level cache. *just saying*
you dont need to add on all of them. just the ones that use table values indexed as player idIs there more examples guys? thank you so much @Gesior.pl I'll apply this "npcHandler.topic[cid] = nil // = nil deletes value from LUA table" to all npcs and will check later if changes
you dont need to add on all of them. just the ones that use table values indexed as player id
everytime you use topic with uid as index you should do what my handsome boy @Gesior.pl says.What you mean? I dont need add in these all npcs?
all npcs I have here works like this:
hastebin
so I need change all? looking this example
function onCreatureDisappear(cid)
npcHandler:onCreatureDisappear(cid)
npcHandler.topic[cid] = nil // = nil deletes value from LUA table
end
After player logout, data connected with player is not removed from memory = memory leaks.
We got table that only grows and some parts of it won't be accessed anymore by program (every relog player gets new unique 'cid').
It's not 'c++ style' memory leak. Where you lose pointer to some object and it's memory is not accessible anymore.
It's LUA, script language with garbage collector, so by definition, it should be not possible to create object that is not accessible from anywhere.
I wouldn't say it's cache. It's table with information needed to execute script. You can't store it any other way. Only problem with this script is that, it does not remove table records after last possible use of given key.
@eldera
If you got some better example. I'm pretty sure a lot of people would like to see it and learn more about LUA optimization problems. That's what we do on forum
npcHandler.topic[cid] = someExpensiveWork();
collectgarbage("setpause", 100)
What are the defaults on thoose values?more than likely you aren't going to fix your memory leaks by optimizing tiny parts of code such as a few table indexes or unneeded variables
if you're really having that big of an issue with ram and truly believe it's from lua you can set the garbage collector to run faster and more aggressively by doing:
the likely issue at hand is you have a memory leak in your sources, which would be a lot bigger in terms of memory usage than a leak in luaLua:collectgarbage("setstepmul", 100) collectgarbage("setpause", 100)
if you're running linux on your server you can use valgrind to detect memory leaks in your sources
i was wrong with setstepmul, lower values makes it slowerWhat are the defaults on thoose values?
20:43 Memory Using: 36848.040039062.
local textMem = collectgarbage("count")
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Memory Using: " .. textMem .. ".")