lua-users home
lua-l archive

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

Hello Lua-addicts:

I have a defined a userdata type (an image) that holds large quantities of
memory. Obviously, this data type should be garbage collected as soon as
possible to release resources.
The structure looks like:

struct image {
  int lx;
  int ly;
  pixelvalue * buffer

The 'buffer' part can be potentially huge, but the only part seen by lua
is the 'image' structure, which is fairly small (in my case 32 bytes) and
thus not considered for garbage collection until it is too late. Make the
computation: even setting the gc threshold to 1 kbyte, I can still
allocate 32 images (32x32=1024) before the GC starts acting. If each image
contained 1 Gb worth of pixels, you get a memory overflow almost

One solution would be making the gc aware of the "true" size allocated
within userdata types. I can write an equivalent of sizeof() that would
return the true size allocated below any given struct, but how do I make
this information available to the GC?

Another solution is of course to deallocate explicitly in my lua scripts
(this is what I am currently doing), but that challenges the mere
presence of a garbage collector, doesn't it?

Are there other simpler solutions I am simply not aware of?

Thanks for helping,