python list memory usage
Question: I am trying to improve the memory usage of my script in python, therefore I
need to know what's RAM usage of my list. I measure the memory usage with

    
    
    print str(sys.getsizeof(my_list)/1024/1024)
    

which hopefully would give me the size of the list in RAM in Mb.

it outputs 12 Mb, however in `top` command I see that my script uses 70% of
RAM of 4G laptop when running.

In addition this list should contain a content from file of ~500Mb.

So 12Mb is unrealistic.

How can I measure the real memory usage?


Answer: `sys.getsizeof` only take account of the list itself, not items it contains.

According to [`sys.getsizeof`
documentation](http://docs.python.org/3/library/sys.html#sys.getsizeof):

> ... Only the memory consumption directly attributed to the object is
> accounted for, not the memory consumption of objects it refers to. ...

Use [Pympler](https://pypi.python.org/pypi/Pympler/):

    
    
    >>> import sys
    >>> from pympler.asizeof import asizeof
    >>>
    >>> obj = [1, 2, (3, 4), 'text']
    >>> sys.getsizeof(obj)
    48
    >>> asizeof(obj)
    176
    

