Memory leaks

Started by Cool, Jul 04, 2017, 01:57 PM

Previous topic - Next topic

Cool

Hi @Devs what about memory leaks in vcmp squirrel official plugin when you guys will fix it or its not going to happen

Xmair


Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

NicusorN5

Quote from: Cool on Jul 04, 2017, 01:57 PMHi @Devs what about memory leaks in vcmp squirrel official plugin when you guys will fix it or its not going to happen
Wot memory leaks? Give us examples.

KAKAN

Quote from: NicusorN5 on Jul 11, 2017, 02:33 PM
Quote from: Cool on Jul 04, 2017, 01:57 PMHi @Devs what about memory leaks in vcmp squirrel official plugin when you guys will fix it or its not going to happen
Wot memory leaks? Give us examples.
for( ;; ) NewTimer("abc",10000,0);
function abc(){ print("SUCKS!"); }
Your server will crash after some time.
oh no

Thijn

Quote from: KAKAN on Jul 11, 2017, 03:18 PM
Quote from: NicusorN5 on Jul 11, 2017, 02:33 PM
Quote from: Cool on Jul 04, 2017, 01:57 PMHi @Devs what about memory leaks in vcmp squirrel official plugin when you guys will fix it or its not going to happen
Wot memory leaks? Give us examples.
for( ;; ) NewTimer("abc",10000,0);
function abc(){ print("SUCKS!"); }
Your server will crash after some time.
And how would you expect them to fix an infinite loop?

Stormeus

Quote from: KAKAN on Jul 11, 2017, 03:18 PM
Quote from: NicusorN5 on Jul 11, 2017, 02:33 PM
Quote from: Cool on Jul 04, 2017, 01:57 PMHi @Devs what about memory leaks in vcmp squirrel official plugin when you guys will fix it or its not going to happen
Wot memory leaks? Give us examples.
for( ;; ) NewTimer("abc",10000,0);
function abc(){ print("SUCKS!"); }
Your server will crash after some time.

Believe it or not, the server actually requires memory to store information about timers. So yes, an infinite loop of timer creation will cause you to run out of memory. :)

NicusorN5

BTW Setting a timer to null, is a memory leak too?

KAKAN

Quote from: Thijn on Jul 11, 2017, 05:01 PMAnd how would you expect them to fix an infinite loop?
ok well, set a limit for it. SLC was talking about it. The timer has a memory leak in it. So, create a few hundred timers and watch it.
oh no

Stormeus

Yes, because that timer runs every 10 seconds forever. The server still needs to keep track of it, of course it's going to use memory. Try to reproduce it using timers that only fire once.

KAKAN

Quote from: Stormeus on Jul 11, 2017, 06:03 PMYes, because that timer runs every 10 seconds forever. The server still needs to keep track of it, of course it's going to use memory. Try to reproduce it using timers that only fire once.
function onPlayerJoin(){
NewTimer("abc",1000,1);
}
Now, lets think that every 1 min, a player joins. Keep using this timer and after some days the server will automagically crash. That timer runs only once.
oh no

Stormeus

Quote from: KAKAN on Jul 11, 2017, 06:57 PM
Quote from: Stormeus on Jul 11, 2017, 06:03 PMYes, because that timer runs every 10 seconds forever. The server still needs to keep track of it, of course it's going to use memory. Try to reproduce it using timers that only fire once.
function onPlayerJoin(){
NewTimer("abc",1000,1);
}
Now, lets think that every 1 min, a player joins. Keep using this timer and after some days the server will automagically crash. That timer runs only once.

function TestFunc1() {
        print("test1");
}

function TestFunc2() {
        print("test2");
}

function End() {
        print("End of test");
}

function MoreTimers() {
        local i;
        for (i = 0; i < 250; i++) {
                NewTimer("TestFunc2", 1000, 1);
        }

        NewTimer("End", 1500, 1);
}

function onScriptLoad() {
        local i;
        for (i = 0; i < 250; i++) {
                NewTimer("TestFunc1", 1000, 1);
        }

        NewTimer("MoreTimers", 1500, 1);

        print("Starting test");
}

There's currently a maximum of 255 timers that can be running at any time on the Squirrel plugin. Here, 250 timers are created that run once, then another timer is created to run another 250 timers before ending. All of the timers get run. No memory gets leaked. If you're leaking timers that run indefinitely, that's another issue entirely.

EK.IceFlake

Quote from: Stormeus on Jul 13, 2017, 04:47 PM
Quote from: KAKAN on Jul 11, 2017, 06:57 PM
Quote from: Stormeus on Jul 11, 2017, 06:03 PMYes, because that timer runs every 10 seconds forever. The server still needs to keep track of it, of course it's going to use memory. Try to reproduce it using timers that only fire once.
function onPlayerJoin(){
NewTimer("abc",1000,1);
}
Now, lets think that every 1 min, a player joins. Keep using this timer and after some days the server will automagically crash. That timer runs only once.

function TestFunc1() {
        print("test1");
}

function TestFunc2() {
        print("test2");
}

function End() {
        print("End of test");
}

function MoreTimers() {
        local i;
        for (i = 0; i < 250; i++) {
                NewTimer("TestFunc2", 1000, 1);
        }

        NewTimer("End", 1500, 1);
}

function onScriptLoad() {
        local i;
        for (i = 0; i < 250; i++) {
                NewTimer("TestFunc1", 1000, 1);
        }

        NewTimer("MoreTimers", 1500, 1);

        print("Starting test");
}

There's currently a maximum of 255 timers that can be running at any time on the Squirrel plugin. Here, 250 timers are created that run once, then another timer is created to run another 250 timers before ending. All of the timers get run. No memory gets leaked. If you're leaking timers that run indefinitely, that's another issue entirely.
I'm guessing that's because you use raw arrays for storing timers. Why don't you use std::vector?

Stormeus

#12
Quote from: EK.IceFlake on Jul 13, 2017, 07:36 PM
Quote from: Stormeus on Jul 13, 2017, 04:47 PM<snip>

There's currently a maximum of 255 timers that can be running at any time on the Squirrel plugin. Here, 250 timers are created that run once, then another timer is created to run another 250 timers before ending. All of the timers get run. No memory gets leaked. If you're leaking timers that run indefinitely, that's another issue entirely.
I'm guessing that's because you use raw arrays for storing timers. Why don't you use std::vector?

Because I only barely knew C++ when the Squirrel plugin was written (fun fact, this was during private betas five years ago) and a lot of the current code is just a mess of patches on top of that. std::list would probably be a better alternative anyway since I don't need random access.

I've been meaning to get around to rewriting the plugin but some other priorities ended up taking precedence.

.

#13
Quote from: KAKAN on Jul 11, 2017, 03:18 PMfor( ;; ) NewTimer("abc",10000,0);
function abc(){ print("SUCKS!"); }

That loop will fail after the first 255 iterations because that's the limit hard-coded in the plugin. The memory leaks will come if you begin to create new timers as you destroy old ones. Let this run for 2-3 minutes and watch your server memory usage:

function xyz(a,b,c,d,e,f,g,h,i,j,k) NewTimer("xyz",10,0,a,b,c,d,e,f,g,h,i,j,k);
for (local n = 0; n < 200; ++n) NewTimer("xyz",10,0,n+1,n+2,n+3,n+4,n+5,n+6,n+7,n+8,n+9,n%2,n*2);

The parameter count plays an important role as the memory is never released. The more parameters and the shorter the timers the faster the memory goes out.

@Stormeus I've made an attempt a while back to fix the timers and they could easily be made compatible with the official implementation. As shown in the various snippets given towards the end of the topic.




And while I was uploading that image, it got to:

.