2

Is there a way, in Lua, to determine the (in memory) size of an object?

I found an article on Gamepedia about Lua object memory sizes, but it is not general and precise.

c70u
  • 119
  • 3
  • 12
  • 1
    "in which a table can be stored" when? Do you have a specific function you call "pushes" the table to that storage? What **exactly** is being stored in that pool? The stringified table? The lua C-side table object itself (how you would do that I don't know). – Etan Reisner Jan 06 '16 at 15:46
  • The MP is used as persistent store(non-volatile memory), to keep settings or other information, so the table is stored when information that is kept there was updated. For example, a client-server system, if the client does not succeed to send the information to server, it should keep the information to send it later, even after the client system reboots. The information is kept in JSON format. – c70u Jan 06 '16 at 20:07
  • That manages to provide some information but avoids answering any of my actual questions. Can you please read my questions carefully and try again? – Etan Reisner Jan 06 '16 at 20:16
  • Yes, there is a functions that "pushes" to the MP. When? After a change in volatile memory (if volatile memory is a queue, then after a dequeue or an enqueue). What exactly is stored in that pool? see the graphical example in the question. – c70u Jan 06 '16 at 21:05
  • So you are storing a JSON stringified version of a table to the pool? With a custom function? So the question is what is the size of the JSON string? – Etan Reisner Jan 07 '16 at 15:41
  • That's **entirely** unrelated to the size of a lua object. That's the size of a string (which happens to be stored in lua when your function gets it). I assume your function uses `lua_tostring` (or similar) to get the actual string data out of lua to store it? – Etan Reisner Jan 07 '16 at 23:40
  • I am not agree that it is entirely unrelated. Yes, what I want is to know how much does that JSON string take in memory, but it is for now. There were similar situations, when I needed to know the size of a table or a function. – c70u Jan 08 '16 at 18:01
  • But that's not what you asked here. Your question here was entirely unrelated to that. That's a reasonable question too but not what it looked like you were asking here. If you want to accept either of the submitted answers then I would suggest rewriting the question to remove the JSON string and the memory pool from the question entirely as they aren't related. – Etan Reisner Jan 08 '16 at 18:10

2 Answers2

4

I would give the same explanation as @NicolBolas, but different answers to the questions.

Is there a way, in Lua, to determine the (in memory)size of an object?

Yes, but you may need to use an external module for that. See my earlier answer and specifically lua-getsize module.

Is there a way, in Lua, to determine if the table to be stored is greater than the MP size?

If you know the size of the table with X elements, you can probably extrapolate to a table with Y elements of approximately the same content, but you wont be able to limit the allocations to a particular size unless you use your own allocator that has that logic.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
3

Is there a way, in Lua, to determine if the table to be stored is greater than the MP size?

No.

Is there a way, in Lua, to determine the (in memory)size of an object?

No.

Lua is not responsible for things like capping memory and so forth. That ought to be handled from the C code that creates and manages the Lua state. So if you have a 16MB limit, then that needs to be built into the lua_State when you call lua_newstate. You pass it an allocation function that needs to keep track of all such allocations. It would also allocate storage from the memory pool, not from the heap.

Of course, the allocator can't tell exactly why an allocation is happening. So there's no way to limit just this one specific table to 16MB, if you intend for the Lua state to also do other things.

If you have such specific memory needs for just this one table, you probably need to allocate and store it in C/C++, and then use the Lua interface to expose it to Lua to read/manipulate.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982