lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Feb 18, 2017 10:24, "Martin" <eden_martin_fuhrspam@gmx.de> wrote:
Hello all,

I offer to make values of table "package.loaded" weak to free
unreachable data at garbage collection cycle.

My usage case is many (over 350) small (about 30 lines) files which
is lua code which typically returns table or function. Conceptual
problem is that result of each of that loaded files occupies
"package.loaded" forever. I think it is not very clean from abstract
point of view.

>From the other side I understand that after making "package.loaded"
weak, multiple loads of the same code may occur. For my usage case
this is not a problem, but you guys may have serious objections
which I'd like to hear.

---
Here is artificial example.

Suppose we have code chunk that creates some big table and uses it
in work:

  local do_work =
    function()
      local result = {}
      for i = 1, 1e6 do result[i] = {} end
      return result
    end

  print(collectgarbage('count'))

  do_work()
  collectgarbage()

  print(collectgarbage('count'))

It does not create memory problems yet.

But now we wish to move do_work() to separate module

  --[[ "garbage_making_module" ]]--
  local do_work =
    function()
      local result = {}
      for i = 1, 1e6 do
        result[i] = {}
      end
      return result
    end

  return do_work()

  --[[ main ]]--
  print(collectgarbage('count'))
  do
    local garbage = require('garbage_making_module')
  end
  collectgarbage()
  print(collectgarbage('count'))

Now no memory is freed!

But if we add

  setmetatable(package.loaded, {__mode = 'v'})

in start of the "main" code, it fixes issue.

-- Martin


This would cause modules to be potentially reloaded every time require() is called, which might cause some interesting issues if a module is doing things like creating files or setting variables in its init.