lua-users home
lua-l archive

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


Shouldn't package.loaded be a weak valued table?  The suggestion was
also here [1].  I came across a situation where I have a long running
process and at times I need to unload modules, including any arbitrary
modules that those modules may have loaded.  More precisely, I want to
reload modules following possible changes to them.  The problem is
that package.loaded is like a global variable and keeps modules cached
even if they are no longer being used.  The typical recommendation
I've seen is to use `package.loaded[name] = nil`, but that doesn't
unload dependent modules.  My solution to this was simply to make
package.loaded a weak valued table, thereby allowing modules to be
properly garbage collected like any other object:

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

True, this might not be a general solution to force modules to be
reloaded if they are currently being used, but that could be a more
complicated case.  For example, forcing it via `package.loaded[name] =
nil` could result in two versions of a module to be in memory at the
same time.  The above also assumes the 5.1 style "module" function is
not being used since that stuffs the module in another global, but
most of my modules don't do that and recent 5.2 work moves away from
that.

[1] http://lua-users.org/lists/lua-l/2006-06/msg00071.html