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

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

问题描述



这是我发现的相关内容。

我自己听过并体验过它:Lua的垃圾收集器可以在游戏中导致严重的FPS下降。到垃圾收集器,在那里例如每创建一个Vector()用户数据对象临时存在,直到收集垃圾。



我知道Python使用引用计数,这就是为什么它不需要像Luas GC那样的巨大的性能进食步骤。




  • 为什么Lua不使用引用计数来获得摆脱垃圾?


解决方案

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

简单示例:一个双向链表。每个节点都有一个指向下一个节点的指针 - 本身指向下一个节点。如果你只是取消引用列表本身并期望它被收集,那么你只是泄露了整个列表 - 节点的 none 有一个引用计数为零,因此它们将互相保持对方活。使用引用计数垃圾收集器时,只要有循环对象,基本上都需要将其视为非托管对象,并在完成时自行处理。



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


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

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.

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.

  • Why doesn't Lua use reference counting to get rid of garbage?

解决方案

Because reference counting garbage collectors can easily leak objects.

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.

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

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

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