1
votes

I have a long time fly program, it is a player. After it started, it will load and run Lua code from net by the server's command. Each code have a unique named module.

Every day, it need load serial different code (i.e Lua modules) and run them. I worried the memory will overflow after a long time...

So, my questions is: does a Lua module can be drop? no longer used module I want drop it.

2

2 Answers

2
votes

For modules written in Lua, setting the entry in package.loaded to nil would probably work. However, it is a hack, and shouldn't be used for arbitrary modules (especially C modules)

From Mike Pall, developer of LuaJIT:

Actually, there's no safe way to unload arbitrary modules, even for plain Lua. C modules may depend on each other and unloading them in the wrong order will wreak havoc. And if there's still a userdata with a __gc C function around and you remove the shared library from the address space with the above method ... guess what happens.

Source

For your case, consider not using module and require, and instead make your own, simpler system that utilizes functions like loadstring

2
votes

Module in Lua is just another table with all the functions and variables inside. If you set it to nil and remove the entry in package.loaded it should be cleaned by the garbage collector.

Here's a function implementing module unloading: http://lua-users.org/lists/lua-l/2009-03/msg00587.html