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

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

问题描述

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

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

所以,我的问题是:Lua 模块可以删除吗?不再使用的模块我想放弃它.

解决方案

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

来自 LuaJIT 的开发者 Mike Pall:

<块引用>

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

来源

对于您的情况,请考虑不使用 modulerequire,而是使用诸如 loadstring 之类的函数制作自己的更简单的系统>

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.

解决方案

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

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

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