• 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!

Solved Memory Leak ~ 12Gb of Ram Usage

darkshin

New old member
Joined
Dec 14, 2010
Messages
231
Reaction score
21
Hello Guys!

Today I've noticed that my server is consuming 12gb of ram. I was wondering why and did a quick search and found that it is related to memory leak in scripts. I wonder what kind of code does that? Do you guys have any examples or any explanation to teach me how to deal with this? Thanks!
 
lua debug library shows you how much memory is being used
for what Lua is used in OTS i guess it isnt the problem
but... npc system sucks
 
Do alot of prints at the server console could also cause a memory leak?
no.

Memory problem from LUA is quite rare, because in most cases bad script will crash or timeout your server. Which is for you?
If its timeout its problem with loop.

What TFS you are using and what operating system?
TFS 1.0 win-64x had some problems, which were fixed later.

However if it seems like everything else is ok, but scripts untested.
You gotta start testing scripts 1 by 1.
but only the scripts what deal with global tables.
so look for things like this table. <<see the dot
or something what you cant search from all the files at once manual input of key and value (talking about these: tableName[key] = value)

If you find these scripts what use global tables.
force execute it 10 times. and then print everything whats inside the table.
look trough the values and see are they all needed.
Also try to break it. (lets say you relog few times and force script function)
If table has values what are not suppose to be there anymore call the garbage collector.
I mean make the script itself on the run turn no longer needed values to nil or make stand alone script what only job is to clean up global tables from no longer needed values.

It was mentioned that addEvents can cause memory leak, but don't know about that, because they are scheduled in source, all we get on return is event key. (unless you updated addevents)
 
Firstly, You should know if your source code ( c++ ) files are edited with stupid coding which makes infinite loops or applying non-important scripts.
Secondly, Yeah I agree with @whitevo about addEvents, and also infinite loops are taking some ram ( but not much ) but when you have 400 players and 50 or more of them are looping ;it will cause lag on server or crashing the server or depending on the ram ( as you said 12 is high ram for a server ) would take mush ram, so try to look all your scripts --> I assure you know what you added which makes loop ( loops are like ( for - do-until - repeat-until ).
Thirdly, There are a sample of scripts which consume some ram like Game.getSpectators -> looping the function too much at same time would cause some problems and ram usage is one of them!

Solution :
By the first condition, try to get a new sources and discuss the codes you want to add with experienced members.
Second&Third one: I think everyone have a server know the scripts he have added and how it work, If they are public scripts you can discuss them within all members in support forum or discussions forum, else they are private you could start a conversation with trusted person but ask for their permission first to do this. @HalfAway is a good person to discuss with, even @Red or @Jo3Bingham are also good. ( I just mentioned them as examples, and I don't say they accept on your own! )

(Stupid scripts) Don't consume the rat usage using a noob script while you can make a simple and even better script to run smoothly!

Regard, Slavi.
 
Some scripts cause annoying problems later on the line. Regardless did you do it or not, its going to be pain in the ass the find the issue.

I have some scritps with these kinds of table, but they are related to quests rewards and daily tasks. I will make some tests on them. By the way I use TFS 1.2 x64. Earlier I've fixed some spells related to the addEvent and the ram usage stopped growing, but considering what you said I should tests the Npcs as well.

Firstly, You should know if your source code ( c++ ) files are edited with stupid coding which makes infinite loops or applying non-important scripts.
Secondly, Yeah I agree with @whitevo about addEvents, and also infinite loops are taking some ram ( but not much ) but when you have 400 players and 50 or more of them are looping ;it will cause lag on server or crashing the server or depending on the ram ( as you said 12 is high ram for a server ) would take mush ram, so try to look all your scripts --> I assure you know what you added which makes loop ( loops are like ( for - do-until - repeat-until ).
Thirdly, There are a sample of scripts which consume some ram like Game.getSpectators -> looping the function too much at same time would cause some problems and ram usage is one of them!

Solution :
By the first condition, try to get a new sources and discuss the codes you want to add with experienced members.
Second&Third one: I think everyone have a server know the scripts he have added and how it work, If they are public scripts you can discuss them within all members in support forum or discussions forum, else they are private you could start a conversation with trusted person but ask for their permission first to do this. @HalfAway is a good person to discuss with, even @Red or @Jo3Bingham are also good. ( I just mentioned them as examples, and I don't say they accept on your own! )

(Stupid scripts) Don't consume the rat usage using a noob script while you can make a simple and even better script to run smoothly!

Regard, Slavi.

Hey Slavi, I've asked about c++ cause I didn't knew about Memory Leak, so Its good to know where it could be, but at sources it should be a infite loop ya? But I don't have it in my sources.
For loops I usually use break, but I didn't knew about the Game.getSpectators.. maybe calling it everytime on onThink should be a problem to Ram?

Usually I don't copy scripts, I've been learning c++ about 7 months, but Lua I am learning it about 2 months. So I just practicing some Ideas. As I've said to Whitedevo, A fix to some spells seems have the memory under control for now.. but I will test the global tables and "daily tasks".
 
no.

Memory problem from LUA is quite rare, because in most cases bad script will crash or timeout your server. Which is for you?
If its timeout its problem with loop.

What TFS you are using and what operating system?
TFS 1.0 win-64x had some problems, which were fixed later.

However if it seems like everything else is ok, but scripts untested.
You gotta start testing scripts 1 by 1.
but only the scripts what deal with global tables.
so look for things like this table. <<see the dot
or something what you cant search from all the files at once manual input of key and value (talking about these: tableName[key] = value)

If you find these scripts what use global tables.
force execute it 10 times. and then print everything whats inside the table.
look trough the values and see are they all needed.
Also try to break it. (lets say you relog few times and force script function)
If table has values what are not suppose to be there anymore call the garbage collector.
I mean make the script itself on the run turn no longer needed values to nil or make stand alone script what only job is to clean up global tables from no longer needed values.

It was mentioned that addEvents can cause memory leak, but don't know about that, because they are scheduled in source, all we get on return is event key. (unless you updated addevents)

About the global tables, It can only cause a memory leak if the script edit them?
What if the global table is just declared and left there to the script consult the variables, because I mostly use these kinds of table: (tableName[key] = value) to declare values like storages and the script take the values from the storages.
 
About the global tables, It can only cause a memory leak if the script edit them?
What if the global table is just declared and left there to the script consult the variables, because I mostly use these kinds of table: (tableName[key] = value) to declare values like storages and the script take the values from the storages.
only if edit them.
 
Is good for you make a clean up often, and review yours systems and scripts.
and disable things to know what is using memory.

Try use something with os.clock, to print on console, like that:
Code:
function lol(t)

for k, v in pairs(t) do
    for k, v in pairs(t) do
        for k, v in pairs(t) do
        end
    end
end
    return print("os clock is: "..os.clock())
end

local t = {1,2,3,4,5,6,7,8,9, " -------" }
lol(t)
 
So, in this case all local variables are automatically set to nil when the functions meet an end, right?
not rly. but next time the scripts executes it rewrites the allocated memory. (at least I think so.. because it rewrites the variable)
 
make use of the garbagecollector this tells you crucial informations needed to know how much ram is currently used by lua
print(collectgarbage("count")) -- will print you the ram used by lua in kbytes

Edit:

You should also combine that with coroutines, so you can save execute a script in a different thread and check meanwhile how much ram it uses or determine if you have infinite loops and stuff like that.
 
Back
Top