What are weak tables?

Asked

Viewed 388 times

-9

What are Weak Tables (weak tables) in version 5.0 Lua? When to use them?

  • this code is a little confused and does not do what you say it does. If show better the real situation maybe give a fix a response.

  • @Bigown In fact he does: local l = 0; for k in next, tables do l = l + 1; end print(l);. The actual situation would be more complicated to understand, as it is related to the class instance.

  • Only in this situation does not occur what you say, so do not have to do anything, is already solved.

  • @bigown No, the intention is to make the key references in the table tables were weak, i.e., being ignored by the garbage collector. There is this, only I want an appropriate response (more or less wiki)

  • Depending on the circumstances, your question does not show what you really want, to tell you the truth this code is getting in the way of more than helping since it has nothing to do with the doubt.

  • @bigown It is a case where weak references would be useful and would help to solve a performance problem. This has a little to be.

  • Your code has no references so it has nothing to do with it. Weak references have nothing to do with performance. Actually if using wrong will have disastrous results. I think you are well lost in this matter it is better not to use what you do not understand.

  • @Bigown It does have references, the table itself tables already uses a reference. This call init {} is a shortcut to init({}), i.e., declares a table for the first argument of init. I know what reference is, and I know that init also a reference.

  • There is a reference. This will not be destroyed because it is global. You talk about having references within tables. The question does not make sense, but I will answer what serves to other people to know about weak references in generic terms.

  • @Bigown You’re talking nothing there be, maybe you didn’t read the code right.

  • 1

    I did, but now that you posted an answer that says exactly what I answered, I realized I already knew what you wanted as an answer, you just don’t know how to ask, the code of your question doesn’t need and there’s no point in using what you want as an answer. If possible, put a code in your answer that makes use within that code posted in the question (not another).

  • 6

    This question is being divided into: http://meta.pt.stackoverflow.com/q/5675/7210

  • 2

    I guess this isn’t memory junk looking good 'cause it’s not for i = 1, 2e2 do the expression 2e2 is equal to 200. Converts 2e2 in this website and you will see that 2e2 is equal to 200 on decimal basis. The e means the number of boxes (0) after the number.

  • @Gato didn’t say anything was memory junk in the question. I’m saying that in the example there is a table with 200 fields that prevent 200 tables from being collected by the language garbage. And I know that 2e2 or 2e+2 is 200.

  • 2

    @Theprohands see if these help links: link 1, link 2 and link 3.

Show 10 more comments

1 answer

12


What you wish to do does not need weak reference. If the code is that, will run something and quit, do not need to do anything for the memory to be released.

If you want to create the table and have it destroyed right away, put it in a local variable. Even if you have to go from one function to another do it with local variables. When no variable is accessing the table it can be destroyed. Local variables care invalidate the reference immediately or the GC will collect the object in the next step.

Using weak reference you have no control over the content, in the first collection may no longer exist the data and if trying to access will have problems, then use weak reference there is a mistake.

To use something global and have control over the lifespan need to do manually.

A generic response according to documentation, but remember that this should only be used when you can run out of data at any time, even before using it.

a = {}
b = {}
setmetatable(a, b)
b.__mode = "v"
tabela = {}
a[1] = tabela
tabela = {}
a[2] = tabela
collectgarbage()
for k, v in pairs(a) do print(v) end

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The way v assigned with setmetatable makes table values as weak references which is what you want. The letter k does the same with the keys.

  • Now you can only give +1 when the answer is edited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.