lua-users home
lua-l archive

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

Ah but we are using lua 4, not lua 5, I think lua_newuserdata is only in lua 5 ?

By the way, it seems in lua 5, lua_newuserdata has completely replaced lua_pushusertag ? I think the problem is still the same, because lua_newuserdata is only taking in account the memory allocated by lua itself, but if the userdata is just a pointer onto a big object which is deleted by the gc methamethod, it will not know about this ... I admit it is arguable that lua should take care at all about the memory usage of object that it has not created itself.

My idea was the following, when the user create some userdata for which the gc metamethod free some memory, he may want to declare the amount of memory that is going to be freed because then it makes sense from a general memory usage point of view.

The problem we had in our application is that at some time we are creating some temporary lua tables and strings, and so we are reaching the gc threshold quite quicky, which is exatly what we wanted (because our memory is limited), but at some other time we only create temporary userdata (for example some userdata containing a pointer on big objects) and the gc threshold will be reached too late, after we run out of physical memory, because the actual memory usage was not counted correctly. Providing the additional used memory information solved the problem.

Vincent Penne.

Luiz Henrique de Figueiredo wrote:

In this project, in particular we have many temporary userdata that are created and forgot every frame. I found that one problem is that lua count the memory used by userdata as only the memory taken to manage it internally.

No if you use lua_newuserdata. This functions allows Lua to allocate and manage
a writeable buffer of any size. When no references exist, it is collected.