lua-users home
lua-l archive

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


Hello,

I have a question regarding tables and garbage collection / memory saving. (I
don't know whether it's relevant but I am using lua 5.1):

As far as I know, when I create some tables:

TableA = {<many entries>}
TableB = {<many entries>}
TableC = {<many entries>}

and I delete one table:

TableB = nil

then lua will free (garbage collect) the memory that was used by TableB.

If I put everything into one table and try the same:

TableMain = {}
TableMain.A = {<many entries>}
TableMain.B = {<many entries>}
TableMain.C = {<many entries>}

TableMain.B = nil

then the memory is not garbage collected by lua. I hope this is correct so
far.

Now what can I do to get one large table with a lot of data without the
disadvantage of high memory consumption?

I have two ideas, and I would like to know whether both work and which one
you would prefer:

A)

TableA = {<many entries>}
TableB = {<many entries>}
TableC = {<many entries>}

TableMain = {}
TableMain.A = TableA
TableMain.B = TableB
TableMain.C = TableC

TableMain.B = nil
TableB = nil

Does garbage collection now work? Is the order of the last two lines
important?

B)

I guess an alternative would be to use metatables to catch any request to
TableMain and redirect it to the other tables.

thank you very much!
Michael