What are weak tables?
To understand weak tables, you need to understand a common problem with garbage collection. The collector must be conservative as possible, so cannot make any assumptions about data; it is not allowed to be psychic. As soon as objects are kept in a table, they are considered referenced and will not be collected as long as that table is referenced. Objects referenced by a weak table will be collected if there is no other reference to them. If a table’s metatable has a __mode field then it becomes weak; __mode may be one or both of ‘k’ (for weak keys) and ‘v’ (for weak values). Putting a value in a table with weak values is in effect telling the garbage collector that this is not a important reference and can be safely collected. Note that strings should be considered a value (like numbers, unlike an object such as a table) for this purpose. This guarantees that string keys not be collected from a table with weak keys.