Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What are weak tables?

tables weak
0
Posted

What are weak tables?

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123