Measure Object Size Accurately in Python - Sys.GetSizeOf not functioning

前端 未结 6 1970
一向
一向 2020-11-30 12:31

I am trying to accurately/definitively find the size differences between two different classes in Python. They are both new style classes, save for one not having sl

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 13:07

    As others have stated, sys.getsizeof only returns the size of the object structure that represents your data. So if, for instance, you have a dynamic array that you keep adding elements to, sys.getsizeof(my_array) will only ever show the size of the base DynamicArray object, not the growing size of memory that its elements take up.

    pympler.asizeof.asizeof() gives an approximate complete size of objects and may be more accurate for you.

    from pympler import asizeof
    asizeof.asizeof(my_object)  # should give you the full object size
    

提交回复
热议问题