Page 1 of 1

memory usage and debug.sethook

Posted: Wed Oct 09, 2024 10:28 pm
by rostok
Hello,
I am writing a game in PICOLOVE hybrid and been trying to reduce per-frame memory allocation to unburden garbage collector. My typical per frame gain is about 37kb, however every couple of seconds it suddenly grows to 100kb. This is not a big issue since GC can handle this without any impact on framerate. I've been trying to narrow it down and find out what exactly is causing these mem alloc bumps. To no avail.

However, I have encountered a strange phenomenon. Once I setup a totally bogus call hook to an empty function

Code: Select all

local function count_calls()
end
debug.sethook(count_calls, "c")
the memory allocation suddenly has no bumps and gets back to steady 37kb per frame.

I would like to know if anyone encountered this as well, what is the reason for this behavior and is it good to ship my game with such hook.

Re: memory usage and debug.sethook

Posted: Wed Oct 09, 2024 10:52 pm
by slime
rostok wrote: Wed Oct 09, 2024 10:28 pm is it good to ship my game with such hook.
No, it'll interact very poorly with JIT compilation, and it'll make your code slower when it works.
rostok wrote: Wed Oct 09, 2024 10:28 pm I've been trying to narrow it down and find out what exactly is causing these mem alloc bumps. To no avail.
Something like https://github.com/pfirsich/jprof might help figure it out.

Re: memory usage and debug.sethook

Posted: Thu Oct 10, 2024 12:05 pm
by pgimeno
In fact, it could be JIT-compiled code. Try jit.off() at the beginning of your main.lua instead of debug.sethook to find out. In any case, I'd say you don't need to worry because 100 kB is very reasonable. In more or less simple programs I've had garbage in the order of tens of MB, and they worked fine even in a Raspberry Pi 3.