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

I can surely agree with you on many of those factors, Gregg. No. Quite the contrary actually; I would like to disagree on most of them. You seem to be talking a lot, using a lot of big words without having any clue about what they mean. I'd like to inform everyone to ignore this guy, I believe he is a troll, and you should therefore not take his advice seriously. It could lead to worsing your server, rather than improving it.
 
I can surely agree with you on many of those factors, Gregg. No. Quite the contrary actually; I would like to disagree on most of them. You seem to be talking a lot, using a lot of big words without having any clue about what they mean. I'd like to inform everyone to ignore this guy, I believe he is a troll, and you should therefore not take his advice seriously. It could lead to worsing your server, rather than improving it.
Well, let's see the meaningful person, show us your opinion, maybe I am troll! and stupid! and you are genius!
 
Well, let's see the meaningful person, show us your opinion, maybe I am troll! and stupid! and you are genius!
I can surely agree with you on many of those factors, Gregg. No. Quite the contrary actually; I would like to disagree on most of them. You seem to be talking a lot, using a lot of big words without having any clue about what they mean. I'd like to inform everyone to ignore this guy, I believe he is a troll, and you should therefore not take his advice seriously. It could lead to worsing your server, rather than improving it.

Hey guys, chill down. Neon has 4 messages, he's a fake to make you mad Slavi.

BACK TO THE TOPIC:

I've seen that the memory usage is getting higher in two different timing cycles and with different sizes, while no player is online.
Should those be onThink functions at monsters or npcs?
 
Hey guys, chill down. Neon has 4 messages, he's a fake to make you mad Slavi.

BACK TO THE TOPIC:

I've seen that the memory usage is getting higher in two different timing cycles and with different sizes, while no player is online.
Should those be onThink functions at monsters or npcs?
first of all monster onThink() function doesn't work if there is no players online and it only starts working if monster is "poked" with action or they have a targetList

Second of all I deleted entire default NPC system from my datapack because I could not bare it. So my guess for your answer is NPC's
 
first of all monster onThink() function doesn't work if there is no players online and it only starts working if monster is "poked" with action or they have a targetList

Second of all I deleted entire default NPC system from my datapack because I could not bare it. So my guess for your answer is NPC's

I will check this later,
Well, now I am testing the collectgarbage with the Lua chat >> https://otland.net/threads/tfs-1-1-luascript-channel.229170/

Ive got this print value in console: 4663.22265625
What should this means?
 
I'm sorry, I'm pretty sure the discussion is passed this, but this misinformation is bothering me...

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

If a script is causing an infinite loop, you'll know it. All it takes is one infinite loop to freeze the entire server until you restart it. It makes no difference if you have one player online, or 500 players.

I implore anyone curious to throw this bit of code into a script and execute it:
Code:
while 1 < 2 do
    print("Looping")
end

This is a very simple example of an infinite loop.
Red
 
If a script is causing an infinite loop, you'll know it. All it takes is one infinite loop to freeze the entire server until you restart it. It makes no difference if you have one player online, or 500 players.

I didn't say it's hard to know! ( I said everyone know the scripts he added to his server)

I have opened forgottenserver ( OTRS MAP ) and made a stupid script (onThink) to do some functions! ( game.getSpectators was one of these noob functions! )( players 0 and ram usage 2.5)
keep in mind my pc has 16 gb of ram and core i7 processor.

when i opened 1 mc ram usage still 2.5;
when i increased to 12 mc ( using another pc ) ram usage was 2.7;

Then I borrowed my brother's pc and opened another 15 mc ( 2.9 ~ 3.1 then after about 1 minute server has freezed )

I repeated all of these again but before it freezed i closed almost of mc's, i saw the ram usage from 3.0 -> 2.6 ( in 1 ~ 2 ) minutes

when I repeated again and stopped the script with about 25 or 30 players online ( I didn't count the mc's this time ) the ram usage was ( 2.5 ~ 2.6 )
It doesn't matter how much players online ( agree with that ) but matters the amount of them are looping.
 
Last edited:
Then I borrowed my brother's pc and opened another 15 mc ( 2.9 ~ 3.1 then after about 1 minute server has freezed )
freeze is not infinite loop.
I once had A monster who was mapping a path to player, but if player was inside a closed area. Monster loaded ENTIRE MAP into table it gave the value few minutes later by saying: "sorry cant find the way to him. I wont cast the spell"
 
freeze is not infinite loop.
I once had A monster who was mapping a path to player, but if player was inside a closed area. Monster loaded ENTIRE MAP into table it gave the value few minutes later by saying: "sorry cant find the way to him. I wont cast the spell"
So give me an explanation of the ram leaking!
 
So give me an explanation of the ram leaking!
Well I'm not rly a coding expert I learned LUA by looking other codes and by getting help from others in this forum. So I might get the concepts of memory leak wrong.
From wiki I see that memory leak is something what was stored in memory and can not be accessed for example table.

In the below function it created 3 values to table every time.
I think what makes this a memory leak is when you only need to access 1 key once and after that you leave it stay there or when you add something in the table, but you are not using all the values.
Code:
table = {}
local ID = 0
function f()
local function generateID()
    ID = ID + 1
    table[ID] = "watever"
end

    for executeCount=1, 3 do
        generateID()
    end
end
 
freeze is not infinite loop.
I once had A monster who was mapping a path to player, but if player was inside a closed area. Monster loaded ENTIRE MAP into table it gave the value few minutes later by saying: "sorry cant find the way to him. I wont cast the spell"

Then the loop was not infinite, it was just slow, d'oh.
 
You can't leak memory in Lua because of the garbage collector. Memory "leaks" whenever you grab a piece of memory from the heap and it becomes either unreachable (zero pointers that point to it) or your code fails to clean it up. In Lua (and many other languages), however, whenever an object is unreachable, it is garbage collected automatically. In spite of being garbage collected, if a script uses huge amounts of memory and that memory never becomes unreachable, that memory will never be freed and your memory usage will spike. It is not, however, a memory leak.

If a Lua script is really the cause of that enormous memory usage, it is probably storing information in a table and never cleaning it up:
Code:
local list = { }

-- something happened and we need to store some info
table.insert(list, info)

If you constantly insert things into that table and never clean it up, memory usage will steadily increase. Whenever you are done with a piece of information stored in a table, you should either remove it with table.remove or list[index] = nil. That way, whatever object was in there can now be collected automatically by Lua - if nothing else points to it.
 
You can't leak memory in Lua because of the garbage collector. Memory "leaks" whenever you grab a piece of memory from the heap and it becomes either unreachable (zero pointers that point to it) or your code fails to clean it up. In Lua (and many other languages), however, whenever an object is unreachable, it is garbage collected automatically. In spite of being garbage collected, if a script uses huge amounts of memory and that memory never becomes unreachable, that memory will never be freed and your memory usage will spike. It is not, however, a memory leak.

If a Lua script is really the cause of that enormous memory usage, it is probably storing information in a table and never cleaning it up:
Code:
local list = { }

-- something happened and we need to store some info
table.insert(list, info)

If you constantly insert things into that table and never clean it up, memory usage will steadily increase. Whenever you are done with a piece of information stored in a table, you should either remove it with table.remove or list[index] = nil. That way, whatever object was in there can now be collected automatically by Lua - if nothing else points to it.

So as far as i understand every empty table that gets created need to be destroyed after?
with
Code:
table.remove or list[index] = nil

Code:
function onCastSpell(savage, isScrewed, isLazy)
local a = {}

~~~~~~~~~~~~
table.insert(a, ~~)
~~~~~~~~
end
I thought if i put it like this that the table gets cleaned as local a gets a empty table assigned on every cast... which cleanses the table instead of stacking values into them. Or does it stack internally memory wise?

I'm sorry for the resurrection of this thread, but i think it's usefull too know. For maybe others too..
 
If a script is causing an infinite loop, you'll know it. All it takes is one infinite loop to freeze the entire server until you restart it. It makes no difference if you have one player online, or 500 players.

I implore anyone curious to throw this bit of code into a script and execute it:
Dude, have you ever heard of recursion?

You can have just something like this:

function loop(a)
a = a + 1
addEvent(loop, 500, a)
end

This is an infinite loop that won't freeze your server and can take many ram of the server.

@OP: try to comment the xml files (actions, movements, creaturescripts, globalevents) one at a time. If the server continues to freeze you'll know it isn't scripts.
 
Back
Top