Re: How is it possible to check how many bytes in memory an instance of a class takes?

William Welliver <[email protected]> Tue, 30 Apr 2019 14:50:26 -0400 (EDT)
Newsgroups gmane.comp.lang.pike.user
Message-ID <[email protected]>
I'm not sure if anyone's answered your question privately, but I can 
provide some information that might be helpful:

As you've noticed, sizeof(object) returns the number of elements within 
the object, not it's size in bytes. Figuring out how much memory an object 
actually takes is tricky, not least because that could mean a number of 
different things (most of the size of the underlying object struct are 
pointers to other bits of memory, as an example).

There are a number of helpful methods in the Debug module... 
memory_usage() and pp_object_usage() in particular seem like they might 
lead you to some answers.

Also, how much memory does your application use before it creates all of 
it's objects? That should give you an idea of the base memory requirement.

If you have garbage, running gc() might give you some indication of this.

Finally, because of the way memory allocation works, freeing objects may 
not necessarily return all of your memory back.

Hope this helps!

Bill

On Fri, 26 Apr 2019, MineMoreGames wrote:

> 
> Basically the title, I don?t think that sizeof gives me what I want but maybe it does and I?m wrong, I want to be able to calculate how
> much memory (RAM) my library takes overall, I?ve written a Discord bot library and it has 20K member objects in cache and it takes
> more than 200MB of RAM, Libraries in languages like Java (JVM) and Javascript (Node) take way less, about 50 ? 70MB RAM.
> 
>  
> 
> When I?m writing the sizeof a user instance to the console it outputs 14.
> 
> So 14 bytes * 20K = 280000 bytes! Which means that it?s not logical for such big of a memory intake to occur, I guess sizeof is not
> what I?m looking for.
> 
>  
> 
> Thank you for any in coming responses on that one and if anyone of you have a Discord account and would like to discuss with me
> about memory optimization in large scale I will be glad!
> 
>  
> 
> ~Amit.
> 
> 
>