Log In  

You can use this function in your program to do some basic memory profiling. Given the limit of 1MB memory for Lua variables, a little profiling will be much needed for many of us ;) It will return the amount of all elements of a table, including the content of sub tables.

It will not show the actual amount of bytes used, would be interesting if there is a way to calculate that? But you can use this as a rough estimation and check if your optimizations show any effect on the table size.

table={}
function table.size(t)
    local size=0
    for k,v in pairs(t) do
     size+=1
     if type(v)=="table" then
        size+=table.size(v)
     end
    end
    return size
end
P#43654 2017-08-26 16:03 ( Edited 2017-08-27 15:20)

I believe stat(0) returns the number of kilobytes currently allocated. It's fractional, so the number of bytes is basically stat(0)*1024. Mind you that'll overflow Pico's signed 16.16 fixed-point numbers if you have more than 32k allocated, so I think that's why it's in KB and not in B.

Note though that this probably doesn't do a garbage collection first, so if you had, say, a 64k string and created a new one with s=s..s, you'd end up with 64k+128k allocated until the next collection swept away the old discarded string.

P#43682 2017-08-27 11:20 ( Edited 2017-08-27 15:21)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 05:29:46 | 0.005s | Q:11