List memory usage

前端 未结 2 925
[愿得一人]
[愿得一人] 2021-01-11 14:03

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          


        
2条回答
  •  不知归路
    2021-01-11 14:41

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

    According to sys.getsizeof documentation:

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

    Use Pympler:

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

提交回复
热议问题