Re: Query on LuaJIT Garbage Collector (GC)

  • From: Javier Guerra Giraldez <javier@xxxxxxxxxxx>
  • To: LuaJIT <luajit@xxxxxxxxxxxxx>
  • Date: Wed, 1 Aug 2018 23:23:14 +0100

On 1 August 2018 at 18:18, Atul Thosar <atulthosar@xxxxxxxxx> wrote:

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"))



the table `t` has 5 million elements, but all of them hold the same
string.  since strings are immutable, there's no point in storing 5
million copies of it, so they are all references to the same string
object.  the memory used is the table itself.  when you clear it, it's
not immediately shrunk.  try assigning a single value after clearing
it, like `t[1]=1`. this might trigger the downsizing of the table.

also, when the table is cleared, the string object can't be discarded.
Since it's a constant in your code, the bytecode holds one more
reference to it, so it's almost permanent.  (unless you release the
code chunk itself).

finally, when you remove the reference to the table (with the
`t=nil`), what you're releasing is the table itself, not the string
object.

Other related posts: