Moon: Make the Moon's garbage collector collect single objects / tables referenced by a [duplicate] table

-4

Let's say I have a table that stores information about 200 tables,

local rand = math.random;

--[=[
  - Uma tabela que armazena uma informação
  - sobre tabelas específicas
  - (Cada campo contém uma chave que referencia uma tabela,
  - e um valor sendo um número randômico de 0 à 50, por exemplo:
  - tables[ {} ] = rand(0, 50))
  ]=]
tables = {};


-- Armazena um número randômico para uma tabela
local function init(t)
    tables[t] = rand(0, 50);
end

for i = 1, 2e2 do
    init {};
end

It seems okay, but the table referenced by tables , in the end, gets 200 fields that together refer 200 tables that needed to be collected by the garbage collector of the Moon.

  

Q: 200 fields? Did each reference a table?
  A: View the loop and call init {} . A table is being declared and referenced in the first argument of the function referenced in init . This function defines a field in tables , its key being the table of the first argument of the call, and its value being a random number. That is, the defined field key references a table.

     

Q: Why did the 200 declared tables need to be collected and why could they not be collected?
  A: Because they will not be recognized by my program. The 200 tables in this case have nothing to do with the program. Imagine if they were tables instantiated by a class. This class could have a table that holds private data for its instances. To do this, each field in this table contains a key that references the instance (respectively a table), and the value of each field is equivalent to the data of its instances. But what if this private data table is preventing instances (tables ) of that class from being collected by the Lua's garbage? For example, a case where the private data table is the only one that is referencing a certain table and still saving its data. The intention is that these single tables referenced within the private data table are collected as junk.

As long as the table referenced by tables is not garbage collected by the Moon, 200 unused tables will not be collected as well. In this example code the program will finish executing and the table will probably be collected as garbage by the Moon, but that is not the purpose, as said, it is an example , the real situation would not be this way . In the real situation, the program will continue to run (and asynchronously) until it is stopped for any occasion.

Is there any way to make the unique tables referenced by the fields of the tables table be garbage collected by the Moon in this case? I also wanted to know if this is only possible with versions of the Moon, larger than 4.0.

    
asked by anonymous 17.01.2017 / 18:07

1 answer

3

In version 5.0 (except in the versions below, I believe) or above there is a way, making the table tables into a weak table . In fact the table will not be a weak table , there will be an option to make the references of the weak fields. This will cause the Lua garbage collector to ignore the presence of each reference, in the keys and values, only in the keys or only in the values, depending on the value of that option.

According to the 2.9.2 section of the ref. of Moon 5.0, when a table has weak keys (or weak keys), the keys to its fields can be collected as garbage. When the key or value of a field is collected (a), that same field is removed from the table (the key space and value), and that makes sense since the value of the key / value field will probably be nil , or typically nothing ... From section 2.9.2:

  

In any case, if either key or value is collected, the whole pair is removed from the table.

That translating into Portuguese would be:

  

In any case, if the key or value is collected, the entire pair is removed from the table.

When a table has weak values (or weak values), then the values of its fields can also be collected as garbage. The same table can have both weak keys and weak values.

This option that allows weak keys or weak values can be set in the __mode field of a table's metatable, and the table will be affected by the option. The option should be a string, which contains the byte 107 ("k"), activates the weak keys mode, and containing the byte 118 ("v") activates the weak values mode. >

In the example code, the __mode field can be something like 'k' , since it makes it possible for the tables referenced in the tables fields to be collected by the Lua garbage. Containing 'v' also makes it possible to collect the values ...

setmetatable(tables, {
    __mode = 'kv'
});

Remembering that unnecessary objects (or tables) can only be collected afterwards, according to two numbers used to control the garbage collector circle: section 2.9 . Or, unless one of these threshold numbers is updated to run the garbage collector at the same time. If that were done, it would be good to undo if it was not a test, but I do not think it is possible to know the current threshold number, but you can choose a good number like before. Section 5.1 of the manual

17.01.2017 / 23:03