Registry / data / objsize

objsize

JSON →
library0.8.0pypypi✓ verified 23d ago

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 objsize
INSTALL
IMPORT
SIG · OBJSIZE
O
objsize
datapythonv0.8.0
Install
1.7s avg
Import
34ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.036s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.032s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

get_deep_size
from objsize import get_deep_size
The primary function is `get_deep_size`.

Demonstrates how to calculate the deep size of a single object or multiple objects using `objsize.get_deep_size()`.

import objsize my_data = {'name': 'Alice', 'age': 30, 'hobbies': ['reading', 'coding']} deep_size_bytes = objsize.get_deep_size(my_data) print(f"The deep size of my_data is: {deep_size_bytes} bytes") another_obj = ['hello', 'world'] total_deep_size = objsize.get_deep_size(my_data, another_obj) print(f"The deep size of multiple objects is: {total_deep_size} bytes")
Debug
Known issues
gotchaBy design, `objsize` attempts to ignore singletons (e.g., `None`) and type objects (classes, modules, functions, lambdas) when calculating deep size. This means they won't contribute to the reported size of the object's subtree, as they are considered shared.
fix
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()`).
affects: All versions
gotchaUnlike some alternatives (e.g., Pympler), `objsize` uses a Breadth-First Search (BFS) approach internally to traverse object subtrees, avoiding recursive calls. This prevents hitting Python's default recursion depth limits, but users migrating from recursive deep-sizing tools should be aware of this architectural difference.
fix
No fix needed; this is a design feature of `objsize` that enhances robustness. Simply be aware of the underlying traversal mechanism.
affects: All versions
gotchaFor 'special objects' or objects with non-standard memory management (e.g., `weakref.proxy` or objects managing off-heap memory), `objsize`'s default calculation might not fully capture their size. You may need to provide custom handlers.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'objsize'
The `objsize` package has not been installed in your Python environment.
fix
Install the package using pip: `pip install objsize`
objsize returns an unexpectedly small or incorrect deep size for certain objects (e.g., torch.Tensor)
The `objsize` library, by default, might not fully traverse the internal memory structures of highly specialized objects (like those from NumPy or PyTorch) that manage their data outside of standard Python object references. This can lead to an underestimation of their 'deep size'.
fix
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.
AttributeError: module 'objsize' has no attribute 'size'
You are attempting to access a non-existent attribute named 'size' directly on the `objsize` module. The primary function for calculating deep size is `get_deep_size`.
fix
Use the `objsize.get_deep_size()` function to calculate the memory consumption of an object: `import objsize; objsize.get_deep_size(my_object)`
Upgrade
Version history
0.8.0latest on PyPI · released Jan 18, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.8 or newer.
Agent activity
7 hits · last 30 days
node
6
Resources
objsize — pip install objsize · libregistry