[Python] Python gets the size of the object

2.11 Get the size of the object

In Python, there are two ways to get the size of memory occupied by an object:
1. Call the built-in obj.__sizeof__ attribute of the object, which returns the memory size of the object itself, excluding other referenced objects.
2. Use the sys.getsizeof(obj) function of the sys module, which returns the memory size of the object itself and some fixed overhead, excluding other objects referenced.

import sys
from icecream import ic

a = [x for x in range(10000)]
b = [x for x in range(100000)]
ic(a.__sizeof__())
ic(sys.getsizeof(b))

14:05:46|> a.sizeof(): 85160
14:05:48|> sys.getsizeof(b): 800984

Guess you like

Origin blog.csdn.net/crleep/article/details/131086299