# Best way to determine the memory footprint of an object

**URL:** <https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827>\
**Category:** Ruby\
**Created:** [March 6, 2007, 8:40am UTC](https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827 "2007-03-06T08:40:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2007, 8:40am UTC](https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827/1 "2007-03-06T08:40:35Z")

</div>

I’d like to be able to estimate how much memory a given object requires.  
What is the best/easiest way to do this? One thing that occurred to me  
was the Marshal.dump the object to a file and look at that, although  
looking at a textual representation of the object could be significantly  
larger (I think).

Any advice is appreciated.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2007, 9:21am UTC](https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827/2 "2007-03-06T09:21:01Z")

</div>

On 06.03.2007 08:40, Wes G. wrote:

> I’d like to be able to estimate how much memory a given object requires.  
> What is the best/easiest way to do this? One thing that occurred to me  
> was the Marshal.dump the object to a file and look at that, although  
> looking at a textual representation of the object could be significantly  
> larger (I think).

Marshal doesn’t text - the output is binary. But there’s another  
problem: what do you regard an object’s size? Is it just the instance?  
Is it the instance plus certain members like Arrays, Strings and  
numbers? Is it the whole graph of objects reachable from your object?  
In any case how do you deal with aliasing, i.e. objects referenced by  
more than one object? etc.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827/3 "2007-03-06T16:07:22Z")

</div>

On Tue, Mar 06, 2007 at 04:40:37PM +0900, Wes G. wrote:

> I’d like to be able to estimate how much memory a given object requires.  
> What is the best/easiest way to do this? One thing that occurred to me  
> was the Marshal.dump the object to a file and look at that, although  
> looking at a textual representation of the object could be significantly  
> larger (I think).
> 
> Any advice is appreciated.

[http://eigenclass.org/hiki.rb?ruby+space+overhead](http://eigenclass.org/hiki.rb?ruby+space+overhead)

A rule of thumb:

for normal objects the memory footprint is around 100 bytes plus 20  
bytes per  
instance variable (applies to objs. with \<55 instance variables).

(Apply recursively if you want to consider referenced objects too.)

See the above link for more details regarding Arrays, Hashes, Symbols,  
Structs, etc…

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/best-way-to-determine-the-memory-footprint-of-an-object/87827/4 "2007-03-06T14:51:02Z")

</div>

On 06/03/07, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to be able to estimate how much memory a given object requires.  
> What is the best/easiest way to do this? One thing that occurred to me  
> was the Marshal.dump the object to a file and look at that, although  
> looking at a textual representation of the object could be significantly  
> larger (I think).
> 
> Any advice is appreciated.

Take a look at object printing code in:

> **[Ruby and Judy](http://t-a-w.blogspot.com/2007/02/ruby-and-judy.html)**
>
> A few days ago I tried to make Ruby a bit faster . Optimizing Fixnum operations and tweaking rb\_call0 seemed pretty straighforward, but ...

It prints reports like:  
IV Table of object Complex(1.0, 2.0)  
Flags = 2  
Class = Complex  
IV\_TBL:

- Bins: 11
- Entries: 2
- Bin 0:
  - Entry
    - Hash: 10802
    - Key: @real
    - Record: 1.0

- Bin 1: empty
- Bin 2: empty
- Bin 3: empty
- Bin 4: empty
- Bin 5: empty
- Bin 6: empty
- Bin 7: empty
- Bin 8:
  - Entry
    - Hash: 10810
    - Key: @image
    - Record: 2.0

- Bin 9: empty
- Bin 10: empty  
Memory use: 88 bytes

It only works with normal objects, not anything special (like strings,  
arrays, hashes, floats etc.), becuase I never needed anything else.

Object uses memory for:

- main data structure (flags, class pointer, table of instance  
variables pointer)
- instance variable table
- some extra storage for non-standard objects
- associated singleton class, if any (if you include it in the count)
- instance variables are objects unless they’re immediate values (if  
you include them in count)
- some undefined memory allocator overhead for all of the above
