Re: Query on LuaJIT Garbage Collector (GC)

  • From: Atul Thosar <atulthosar@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 1 Aug 2018 22:48:18 +0530

Thanks a lot for your response and apologize delay from my side for
responding back.
Not sure I understand all, but I started looking into my code critically. I
have extracted minimal version of my application and trying to check how
collectgarbage("collect") may help. Here is the program

-- Program Starts
t = {}

for i = 1, 5000000 do
  t[i] =
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end

print (collectgarbage("count"))

--[[ Block #1
for i = 1, 5000000 do
  t[i] = nil
end
--]]

-- print("Table Elements: " .. #t)

--[[ Block #2
t = nil
--]]

print (collectgarbage("collect"))
print (collectgarbage("count"))

-- Program Ends

If I run above program as is, output I see

131111.40722656
0
131106.86230469

If I un-comment Block #1 only, output is same

131111.54003906
0
131106.99511719

If I uncomment Block #2 only, output is

131111.42285156
0
34.7138671875


This behavior I could not understand. In Block #1, I am explicitly making
all table elements nil, but GC could not able to clean up them. I also
tried by declaring Table t as setmetatable(t, {__mode = 'v'}) but no use.
In fact tried k and kv options as well, but no use again.

Am I doing anything wrong here? Or do I need to do anything extra?

Thanks in advance.



--
BR,
Atul


On 24 July 2018 at 19:04, szbnwer@xxxxxxxxx <szbnwer@xxxxxxxxx> wrote:

hi there :)

2018-07-24 6:29 GMT+00:00 Atul Thosar <atulthosar@xxxxxxxxx>:
BTW, in luaJIT can I set the memory limit (Something similar to what is
in
Java).

there were some words around this topic in recent talks on lua-l, i
think you can search the list for '__gc' and/or 'collectgarbage'
backwards in time, in case you havent seen that conversation, i think
it was the 'with' statement proposal topic for finalizers. however you
have no straightforward soulution for what you want if im right...

so you can check the actual memory consumption via
collectgarbage'count', but that cant limit, just observe; you can fine
tune and/or run often collectgarbage, but that is just for keeping on
minimum; the best you can do is to introspect your app for possible
leaks and/or bottlenecks, and manually alloc/free resources, but that
most likely something that the jit wont like (im not sure, but i think
its fine via ffi)... and finally there is a real external solution,
you can limit the resources on linux (possibly elsewhere too) but that
will crash your app if running out of free space.... however you can
give it a supervisor that can restart your app when it crashes, but
you can solve that without crash via collectgarbage'count' and
os.exit(123) and the supervisor will know that 123 exit code stands
for restart...

btw gc of lua and luajit differs i think, but the behavior of the api
is the same, so collectgarbage() collects whatever its able to
collect. jit.flush is an another thing, that clears jit traces, but
that is a bit good for memory and much wrong for performance, and
probably jit traces are what making your memory consumption looks
weird (im just using luajit for the performance and ffi, and im trying
to learn whatever i just can about it, but my knowledge is kinda weak,
so dont rely on my words without making yourself sure about anything
im saying about whatever around :D )

an another stuff you could do is to check out raptorjit, it is a
luajiit fork, and i believe it can be a drop-in replacement. one of
its aims is for long living apps like heavy load web services, to not
let the jit mark traces blacklisted, and when its ever done then it
starts a butterfly-effect with other traces that are involved... i
believe what you see around the memory is about the jit traces born
and die by the time... probably you could check out result of jit.off,
and if the memory consumption will be stable, then you will know that
its not a mistake in your app, but the game of the jit... anyway the
author of raptorjit or anyone around will be able to tell you more
precisely further info, however i only wanted to give you some
pointers :D

i believe you need to observe, and refactoring, cuz jit takes some
best practices to reach its optimum, but others around are much better
in this game than me :)

i hope i could write understandable stuffs based on my wrecked english
and my here and there weak knowledge :D

have fun, good luck and all the bests! :)


Other related posts: