Page 2 of 2

Re: Puzzled by a potential memory leak

Posted: Sun May 14, 2023 1:51 pm
by Sasha264
UnixRoot wrote: Sun May 14, 2023 10:00 am If I collect every frame, the memory consumption stays at a stable level for hours, but it lags really bad. It's not a real leak, it's really a garbage problem.
Ok! Back to collecting garbage then =)

Have you tried tweaking the numbers?
  1. The first number will be the 5 in collectgarbage("step", 5). It controls the amount of garbage collected in an unspecified way, so it can be tuned for each specific application.
  2. The second number is the maximum number of collectgarbage("step", ...) calls that can be performed each frame. Fewer calls with a larger size will result in less total garbage collection time. More calls with a smaller size will result in more predictable garbage collection time per frame.
  3. The third parameter sets the maximum time each frame for garbage collection to be done.
  4. The fourth parameter, used in the Batteries manual_gc.lua, is the memory threshold after which a full collectgarbage() will be triggered, causing a massive stutter. However, if this only occurs like once in every 2 hours, it may not be a problem. This adds a layer of protection to the whole process.

Re: Puzzled by a potential memory leak

Posted: Mon May 15, 2023 6:56 am
by UnixRoot
I think my problem could be LuaJIT and/or FFI related. I just found this post with almost the same behavior.

viewtopic.php?t=91198

I have some similar problems like @grump had.

If I run

randomFunction1()
randomFunction2()

it produces a lot of garbage and runs slow. If I instead run

randomFunction2()
randomFunction1()

It's fast again and the garbage doesn't go crazy.

Oh boy, I really start to hate the unpredictable and strange behavior of LuaJIT.

Re: Puzzled by a potential memory leak

Posted: Mon May 15, 2023 7:21 am
by pgimeno
There are some tips there for how to tweak JIT settings. I doubt you have a "diamond pattern" as in that thread, so maybe those tweaks are enough for you.

Re: Puzzled by a potential memory leak

Posted: Mon May 15, 2023 4:39 pm
by UnixRoot
This actually helped a lot. It's not perfect, but the stuttering is gone and garbage production is now "only" 10 MB per minute, instead of almost 500 MB.