memory size of Python data structure

前端 未结 1 948
长情又很酷
长情又很酷 2021-02-01 21:34

How do I find out the memory size of a Python data structure? I\'m looking for something like:

sizeof({1:\'hello\', 2:\'world\'})

It is great i

相关标签:
1条回答
  • 2021-02-01 22:14

    Have a look at the sys.getsizeof function. According to the documentation, it returns the size of an object in bytes, as given by the object's __sizeof__ method.

    As Daniel pointed out in a comment, it's not recursive; it only counts bytes occupied by the object itself, not other objects it refers to. This recipe for a recursive computation is linked to by the Python 3 documentation.

    0 讨论(0)
提交回复
热议问题