lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Wed, 11 Apr 2018, 12:17 Francisco Olarte, <> wrote:
On Wed, Apr 11, 2018 at 11:17 AM, Dibyendu Majumdar
<> wrote:
> One practical issue currently with Lua's GC is it slack of knowledge
> about memory held by userdata objects. This makes the GC calculation
> pretty erroneous - and userdata objects are pretty common I presume.
> Is there a way to make the GC aware of the memory held by a userdata
> object - maybe through an C API call?

The GC already knows about part of the memory held by the object.

The other is difficult to define.

I mean. We use C++. We put the main object into the lua heap, lua
knows about it. But our objects hold other memory consuming resources.
Some are owned, some are shared, some are ref counted, some are not.
And some are not even ours, like windows handles, file buffers and the
like, and some hold more resources. It would be very difficult for us
to tell you how much memory an object uses from C++.

A possible solution could be if you we're able to hint the GC a virtual size for the userdata.

I'd had the same issue with calls to lua from C++ with userdata. The userdata only contained a std::shared_ptr, so it was small for lua, but it contained "virtually" a data chunk that could be a few MB up to 256 MB.
Under some conditions up to 1000 of them where kept alive until the GC decided to clean up some of them. We "solved" it by making the GC very agressive, to make sure unused reference were cleaned up fast.
I've not yet found a better solution.
> In my view this would be a very useful enhancement to Lua.

It could be, but you would need to make it much more smart to take
into account memory external to lua heap. AFAIK the GC works only
across a sinle lua state.

Francisco Olarte.