Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

孙世龙 sunshilong
Hi, list

Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

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.4 function or 3rd party library that could help with
this.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

Matthew Wild
Hi,

On Sun, 27 Sep 2020 at 02:42, 孙世龙 sunshilong <[hidden email]> wrote:
>
> Hi, list
>
> Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

Simple? No :)

But yes, you can use this module:
https://luarocks.org/modules/siffiejoe/getsize to tell you the size of
the table's internal data structures.

However in most applications if there are objects that are referenced
by the table (and possibly *only* by the table) you likely want to
include the size of those objects too.

> 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.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

Matthew Wild
On Sun, 27 Sep 2020 at 11:50, Matthew Wild <[hidden email]> wrote:
> However in most applications if there are objects that are referenced
> by the table (and possibly *only* by the table) you likely want to
> include the size of those objects too.
>
> > 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.

Sorry, hit send too early. Was going to say here, there is no way to
add up all the sizes of these referenced objects without iteration.

Regards,
Matthew
Reply | Threaded
Open this post in threaded view
|

Re: Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

Ką Mykolas
Also, external tracing is always an option. Think of valgrind and friends.

On Sun, Sep 27, 2020, 13:55 Matthew Wild <[hidden email]> wrote:
On Sun, 27 Sep 2020 at 11:50, Matthew Wild <[hidden email]> wrote:
> However in most applications if there are objects that are referenced
> by the table (and possibly *only* by the table) you likely want to
> include the size of those objects too.
>
> > 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.

Sorry, hit send too early. Was going to say here, there is no way to
add up all the sizes of these referenced objects without iteration.

Regards,
Matthew
Reply | Threaded
Open this post in threaded view
|

Re: Is there a (simple) way to get the memory usage of a Lua object (e.g table)?

孙世龙 sunshilong
In reply to this post by Matthew Wild
Hi, Matthew Wild

>However in most applications if there are objects that are referenced
>by the table (and possibly *only* by the table) you likely want to
>include the size of those objects too.
Could you please explain that in simpler words?

Do you mean something like this:
subTable1 = {};
subTable2 = {};

tabTop = {};
tabTop[1] = subTable1;
tabTop[2] = subTable2;
When I want to know the memory size used by tabTop, I need to iterate
through each sub table?

But the table is very simple, e.g:
simTab={};
simTab["score"] = 100;
simTab["name"] = "Tom";
the module aforementioned could directly figure out the memory size
used by simTab.

Looking forward to hearing from you.
Best wishes
Sunshilong
On Sun, Sep 27, 2020 at 6:51 PM Matthew Wild <[hidden email]> wrote:

>
> Hi,
>
> On Sun, 27 Sep 2020 at 02:42, 孙世龙 sunshilong <[hidden email]> wrote:
> >
> > Hi, list
> >
> > Is there a (simple) way to get the memory usage of a Lua object (e.g table)?
>
> Simple? No :)
>
> But yes, you can use this module:
> https://luarocks.org/modules/siffiejoe/getsize to tell you the size of
> the table's internal data structures.
>
> However in most applications if there are objects that are referenced
> by the table (and possibly *only* by the table) you likely want to
> include the size of those objects too.
>
> > 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.