Why does Lua use a garbage collector instead of re

2019-03-11 07:09发布

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?

8条回答
叛逆
2楼-- · 2019-03-11 07:26

In general, reference counting isn't an exact substitute for garbage collection because of the potential of circular references. You might want to read this page on why garbage collection is preferred to reference counting.

查看更多
趁早两清
3楼-- · 2019-03-11 07:29

While others have explained why you need a garbage collector, keep in mind that you can configure the garbage collection cycles in Lua to either be smaller, less frequent, or on demand. If you have a lot of memory allocated and are busy drawing frames, then make the thresholds very large to avoid a collection cycle until there is a break in the game.

Lua 5.1 Manual on garbage collection

查看更多
闹够了就滚
4楼-- · 2019-03-11 07:33

Reference Counting alone is not enough for a garbage collector to work correctly because it does not detect cycles. Even Python does not use reference counting alone.

Imagine that objects A and B each hold a reference to each other. Even once you, the programmer no longer hold a reference to either object, reference counting will still say that objects A and B have references pointing to them.

There are many different garbage collecting schemes out there and some will work better in some circumstances and some will work better in other circumstances. It is up to the language designers to try and choose a garbage collector that they think will work best for their language.

查看更多
Fickle 薄情
5楼-- · 2019-03-11 07:34

Take a look at some of the CPython sources. A good portion of the C code is Py_DECREF and Py_INCREF. That nasty, tedious and error-prone book keeping just goes away in Lua.

If required, there's nothing to stop you writing Lua modules in C that manage any heavy, private allocations manually.

查看更多
Deceive 欺骗
6楼-- · 2019-03-11 07:35

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.

查看更多
爷的心禁止访问
7楼-- · 2019-03-11 07:37

You might also be interested in the Lua Gem about optimization which also has a part that handles garbage collection.

查看更多
登录 后发表回答