Lua and C userdata garbage collection

In my game engine, I show the Vector and Color objects for Lua using userdata.

Now, for every even locally generated vector and color from Lua scripts, Luas memory usage is slightly increased, it does not drop until the garbage collector is started.

The garbage collector calls up a small bench in my game.

Shouldn't you remove Vector and Color objects right away if they are used only as arguments? For example, for example:myObject:SetPosition( Vector( 123,456 ) )

They're not right now - Lua memory usage increases to 1.5 MB per second, then the lag drops, and it goes back to about 50 KB.

  • How can I solve this problem, is it even solvable?
+3
source share
4 answers

Lua , userdata, - . , . ( lua_gc(L, LUA_CGSTEP, ...)), , .

- . , SetPosition, API , , , :

myObject:SetPosition(123, 456)
+2

lua_setgcthreshold(L,0), .

: 5.1 :

int lua_gc (lua_State *L, int what, int data);

Controls the garbage collector.

This function performs several tasks, according to the value of the parameter what:

    * LUA_GCSTOP: stops the garbage collector.
    * LUA_GCRESTART: restarts the garbage collector.
    * LUA_GCCOLLECT: performs a full garbage-collection cycle.
    * LUA_GCCOUNT: returns the current amount of memory (in Kbytes) in use by Lua.
    * LUA_GCCOUNTB: returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024.
    * LUA_GCSTEP: performs an incremental step of garbage collection. The step "size" is controlled by data (larger values mean more steps) in a non-specified way. If you want to control the step size you must experimentally tune the value of data. The function returns 1 if the step finished a garbage-collection cycle.
    * LUA_GCSETPAUSE: sets data as the new value for the pause of the collector (see §2.10). The function returns the previous value of the pause.
    * LUA_GCSETSTEPMUL: sets data as the new value for the step multiplier of the collector (see §2.10). The function returns the previous value of the step multiplier.
+1
+1

Remember that Lua does not know until you save these objects - you could put them in a table in the registry, for example. You did not even notice the consequences of collecting 1.5 MB, there is another problem.

In addition, you really are a waste, creating a new facility for this. Remember that in Lua every object must be dynamically allocated, so you call malloc .. to make a Vector object to store two numbers? Write your function to take a pair of numeric arguments as an overload.

0
source

Source: https://habr.com/ru/post/1790952/


All Articles