The objsize Python package provides functionality for traversing an object's subtree and calculating its total memory consumption in bytes (deep size). It leverages Python's internal Garbage Collection (GC) implementation, designed to ignore commonly shared objects like singletons and type objects for more accurate deep sizing. It's currently in version 0.8.0 and generally has infrequent releases.
pip install objsizeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to calculate the deep size of a single object or multiple objects using `objsize.get_deep_size()`.
If you need to include sizes for specific shared objects or functions, you might need to implement a custom filter function (e.g., by modifying or replacing `objsize.traverse.shared_object_or_function_filter()`).
No fix needed; this is a design feature of `objsize` that enhances robustness. Simply be aware of the underlying traversal mechanism.
For such cases, the library allows users to supply custom methods for size calculation, referent identification, or object filtering to ensure accurate deep size measurement. Consult the library's documentation for details on custom handlers.
Install the package using pip: `pip install objsize`
Provide custom `get_size_func` and/or `get_referents_func` to `objsize.get_deep_size` to instruct it on how to correctly calculate the size or find referents for such objects. For example, for a `torch.Tensor` you might need to account for its underlying storage.
Use the `objsize.get_deep_size()` function to calculate the memory consumption of an object: `import objsize; objsize.get_deep_size(my_object)`