Size of objects in memory during an IPython session (with Guppy?)
Question: I recall [reading](http://stackoverflow.com/questions/563840/how-can-i-check-
the-memory-usage-of-objects-in-ipython?rq=1) that it is hard to pin down the
exact memory usage of objects in Python. However, that thread is from 2009,
and since then I have read about various memory profilers in Python (see the
examples in [this thread](http://stackoverflow.com/questions/1331471/in-
memory-size-of-python-stucture)). Also, IPython has matured substantially in
recent months (version 1.0 was released a few days ago)

IPython already has a magic called `whos`, that prints the variable names,
their types and some basic Data/Info.

In a similar fashion, is there any way to get the size in memory of each of
the objects returned by `who` ? Any utilities available for this purpose
already in IPython?

# Using Guppy

[Guppy](http://guppy-pe.sourceforge.net/) (suggested in [this
thread](http://stackoverflow.com/questions/110259/which-python-memory-
profiler-is-recommended)) has a command that allows one to get the
**cummulative** memory usage **per object type** , but unfortunately:

  1. It does not show memory usage **per object**
  2. It prints the sizes in bytes (not in human readable format)

For the second one, it may be possible to apply `bytes2human` from [this
answer](http://stackoverflow.com/a/13449587/283296), but I would need to first
collect the output of `h.heap()` in a format that I can parse.

But for the first one (the most important one), is there any way to have Guppy
show memory usage **per object**?

    
    
    In [6]: import guppy
    
    In [7]: h = guppy.hpy()                                                                                                                                     
    
    In [8]: h.heap()
    Out[8]: 
    Partition of a set of 2871824 objects. Total size = 359064216 bytes.
     Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
         0 522453  18 151469304  42 151469304  42 dict (no owner)
         1 451503  16 36120240  10 187589544  52 numpy.ndarray
         2 425700  15 34056000   9 221645544  62 sklearn.grid_search._CVScoreTuple
         3 193439   7 26904688   7 248550232  69 unicode
         4 191061   7 22696072   6 271246304  76 str
         5 751128  26 18027072   5 289273376  81 numpy.float64
         6  31160   1 12235584   3 301508960  84 list
         7 106035   4  9441640   3 310950600  87 tuple
         8   3300   0  7260000   2 318210600  89 dict of 0xb8670d0
         9   1255   0  3788968   1 321999568  90 dict of module
    <1716 more rows. Type e.g. '_.more' to view.>
    


Answer: Why not use something like:

    
    
    h.heap().byid
    

But this will only show you immediate sizes (i.e. not the total size of a list
including the other lists it might refer to).

If you have a particular object you wish to get the size of you can use:

    
    
    h.iso(object).domisize
    

To find the approximate amount of memory that would freed if it were deleted.

