I'd like to find out how much memory a Lua table is using - without iterating through the table contents and counting up the usage. Is there a Lua 5.1 function or 3rd party library that could help with this.
You can monitor the memory usage of Lua by calling collectgarbage("count")
or gcinfo(
) in the appropriate locations throughout the code (e.g. before and after insert operations). There's no trivial way to get the size of a table.
There is no function for this task. Why do you want to do this? What are you trying to achieve?
Wouldn't something like this or this help?
2016 Update: see also: http://www.lua.org/wshop15/Musa2.pdf
You could do something like this:
local pre = collectgarbage("count")
local table = {1, 2, 3, 4, 5}
local aft = collectgarbage("count")
local probablyTableSize = aft - pre
print(probablyTableSize)
Do note though, I am not too sure if this would be accurate outside of plain testing environments with a lot of things going on in the background. There is a slight chance that more memory was added/removed when we were declaring the table
variable.
This may be redundant, but what you could do in that case would be to get the mean or the median of multiple attempts and see what happens.
© 2022 - 2024 — McMap. All rights reserved.