{"id":2141,"library":"objsize","title":"objsize","description":"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.","status":"active","version":"0.8.0","language":"en","source_language":"en","source_url":"https://github.com/liran-funaro/objsize","tags":["object-size","memory","deep-size","traversal","debugging","garbage-collection"],"install":[{"cmd":"pip install objsize","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.8 or newer.","package":"python","optional":false}],"imports":[{"note":"The primary function is `get_deep_size`.","symbol":"get_deep_size","correct":"from objsize import get_deep_size"}],"quickstart":{"code":"import objsize\n\nmy_data = {'name': 'Alice', 'age': 30, 'hobbies': ['reading', 'coding']}\ndeep_size_bytes = objsize.get_deep_size(my_data)\n\nprint(f\"The deep size of my_data is: {deep_size_bytes} bytes\")\n\nanother_obj = ['hello', 'world']\ntotal_deep_size = objsize.get_deep_size(my_data, another_obj)\nprint(f\"The deep size of multiple objects is: {total_deep_size} bytes\")","lang":"python","description":"Demonstrates how to calculate the deep size of a single object or multiple objects using `objsize.get_deep_size()`."},"warnings":[{"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()`).","message":"By 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"No fix needed; this is a design feature of `objsize` that enhances robustness. Simply be aware of the underlying traversal mechanism.","message":"Unlike 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"For '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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}