如何删除Lua模块的所有代码和内存空间 [英] How to drop all code and memory space of a Lua module

查看:519
本文介绍了如何删除Lua模块的所有代码和内存空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的飞行程序,它是一个播放器.启动后,它将通过服务器的命令从net加载并运行Lua代码.每个代码都有一个唯一的命名模块.

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.

每天,它需要加载序列不同的代码(即Lua模块)并运行它们.我担心长时间后内存会溢出...

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...

所以,我的问题是:是否可以删除Lua模块?不再使用的模块,我要删除它.

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

推荐答案

对于用Lua编写的模块,将package.loaded中的条目设置为nil可能会起作用.但是,这是一个hack,不应该用于任意模块(尤其是C模块)

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)

来自LuaJIT开发人员Mike Pall:

From Mike Pall, developer of LuaJIT:

实际上,没有安全的方法可以卸载任意模块,即使 用于普通的Lua. C模块可能相互依赖并卸载 他们以错误的顺序将造成严重破坏.如果还有 带有__gc C函数的userdata,您将删除共享 使用上述方法从地址空间中提取库...猜测 会发生什么.

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.

来源

对于您的情况,请考虑不使用modulerequire,而是使用诸如loadstring

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

这篇关于如何删除Lua模块的所有代码和内存空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆