为什么 Lua 使用垃圾收集器而不是引用计数? [英] Why does Lua use a garbage collector instead of reference counting?

查看:28
本文介绍了为什么 Lua 使用垃圾收集器而不是引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己听说并体验过:随着脚本部分的增长,Lua 的垃圾收集器会导致游戏的 FPS 严重下降.

I've heard and experienced it myself: Lua's garbage collector can cause serious FPS drops in games as their scripted part grows.

这是我发现的与垃圾收集器相关的内容,例如,在其中创建的每个 Vector() 用户数据对象都暂时存在,直到被垃圾收集为止.

This is as I found out related to the garbage collector, where for example every Vector() userdata object created temporarily lies around until getting garbage collected.

我知道 Python 使用引用计数,这就是为什么它不需要像 Luas GC 那样庞大的、消耗性能的步骤.

I know that Python uses reference counting, and that is why it doesn't need any huge, performance eating steps like Luas GC has to do.

  • 为什么 Lua 不使用引用计数来清除垃圾?

推荐答案

因为引用计数垃圾收集器很容易泄漏对象.

Because reference counting garbage collectors can easily leak objects.

简单的例子:双向链表.每个节点都有一个指向下一个节点的指针 - 并且下一个节点指向它本身.如果你只是取消引用列表本身并期望它被收集,你就泄露了整个列表 - none 节点的引用计数为零,因此它们都会相互保留活.使用引用计数垃圾收集器,任何时候你有一个循环对象,你基本上都需要把它当作一个非托管对象,并在你完成后自己明确地处理它.

Trivial example: a doubly-linked list. Each node has a pointer to the next node - and is itself pointed to by the next one. If you just un-reference the list itself and expect it to be collected, you just leaked the entire list - none of the nodes have a reference count of zero, and hence they'll all keep each other alive. With a reference counting garbage collector, any time you have a cyclic object, you basically need to treat that as an unmanaged object and explicitly dispose of it yourself when you're finished.

请注意,除了引用计数之外,Python 还使用了适当的垃圾收集器.

Note that Python uses a proper garbage collector in addition to reference counting.

这篇关于为什么 Lua 使用垃圾收集器而不是引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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