get_obj_size¶
- get_obj_size(obj: Any, serialize: bool = True) int[source]¶
Estimate the size of a Python object.
If serialize is True, returns the size of the serialized object. Note that this is NOT the same as the object size in memory.
When serialize is False, returns the memory consumption of the object via the asizeof method of Pympler (https://pypi.org/project/Pympler). An alternative is the get_deep_size() function from objsize (https://pypi.org/project/objsize).
The serialization-based approach was a fallback solution for the issues described in PIPE-1698/PIPE-2877. The asize.py bug described in PIPE-2877 remain unresolved as of Pympler ver 1.1.
- See the GH issues for details and PIPE-1698 and PIPE-2877 for background:
https://github.com/pympler/pympler/issues/155 https://github.com/pympler/pympler/issues/151
- Parameters:
obj -- The Python object to measure.
serialize -- If True, measure serialized size; if False, measure memory size using Pympler.
- Returns:
The size of the object in bytes.
- Raises:
Exception -- If serialize is False and Pympler is not installed.