Install & Compatibility
Where this runs
tested against v1.1 · 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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.042s · 18.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.040s · 19MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
asizeof
✓ from pympler import asizeof
For sizing individual Python objects recursively.
muppy
✓ from pympler import muppy
For on-line monitoring of all objects in a Python application.
summary
✓ from pympler import summary
To format and print summaries of object lists obtained from muppy or classtracker.
tracker
✓ from pympler import tracker
For tracking changes in overall memory or specific class instances over time.
This quickstart demonstrates basic memory profiling using `pympler.asizeof` to get the recursive size of an object, and `pympler.muppy` and `pympler.summary` to get a snapshot and diff of all objects in memory. It also includes a simplified example of `pympler.tracker` to show changes over time.
from pympler import asizeof, muppy, summary
class MyObject:
def __init__(self, data):
self.data = data
self.large_list = [0] * 1000 # A list consuming some memory
# 1. Measure the size of an individual object recursively
obj = MyObject('some string data')
print(f"Size of obj: {asizeof.asizeof(obj)} bytes")
# 2. Get a summary of all objects in memory
all_objects_before = muppy.get_objects()
sum1 = summary.summarize(all_objects_before)
print("\n--- Memory Summary (Before) ---")
summary.print_(sum1, limit=5) # Print top 5 largest object types
# Create more objects
my_list = [MyObject(f'data_{i}') for i in range(10)]
# 3. Get a diff in memory usage
all_objects_after = muppy.get_objects()
sum2 = summary.summarize(all_objects_after)
print("\n--- Memory Summary (After, diff) ---")
diff = summary.diff(sum1, sum2)
summary.print_(diff, limit=5) # Print top 5 changes
# Example of tracker for change over time (simplified)
tr = tracker.SummaryTracker()
# Perform some operations that might create new objects
def create_temp_objects():
local_list = ["temp_str"] * 500
create_temp_objects()
print("\n--- Tracker Diff after create_temp_objects() ---")
tr.print_diff(limit=5)
pympler --version
Debug
Known issues
breakingPympler dropped support for Python 2.7 and Python 3.5 starting with version 1.0. Older Python versions require Pympler < 1.0.fixUpgrade to Python 3.6+ or use an older Pympler version (e.g., `pip install 'Pympler<1.0'` for Python 2.7/3.5).
affects: 1.0 and later
gotchaRepeated calls to `muppy.get_objects()` in a tight loop can themselves consume significant memory, potentially masking or exacerbating actual memory leak issues. The `get_objects()` function collects references to all live Python objects.fixUse `tracker.SummaryTracker` and `print_diff()` for analyzing memory changes over time, which is designed for efficient diffing. If `muppy.get_objects()` is necessary, ensure careful management of the returned object list to avoid accumulating references.
affects: All versions
gotchaThe `asizeof` module's accuracy can be affected by internal CPython object layout changes in newer Python versions, potentially leading to discrepancies in reported sizes for certain object types.fixBe aware of this limitation for critical memory analysis. Cross-reference with `sys.getsizeof` for shallow sizes or consider using `tracemalloc` for different perspectives on memory allocation if absolute precision for all object types is crucial.
affects: Potentially some newer Python 3.x versions (e.g., Python 3.11, 3.12, 3.13+), specifically noted as an open issue for CPython's latest object layout changes.
gotchaPympler operations, particularly those that traverse the entire object graph (e.g., `muppy.get_objects()`, `asizeof.asizeof()` on complex objects), can be computationally intensive and may introduce a noticeable performance overhead in performance-critical applications.fixUse Pympler primarily during development and testing phases. In production, use sparingly or integrate only highly optimized, targeted checks. Avoid calling such functions frequently in performance-sensitive code paths.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pympler'
The `pympler` package is not installed in the Python environment or is not accessible in the current Python path.
fixInstall the package using pip: `pip install pympler`
AttributeError: __flags__
This error often occurs when `pympler.muppy` or `pympler.asizeof` attempts to inspect an object that lacks the expected `__flags__` attribute, or when running on an incompatible Python interpreter like PyPy. It can be triggered by specific external library objects (e.g., VTK objects).
fixUpdate Pympler to the latest stable version (e.g., `pip install --upgrade pympler`) as compatibility issues are often addressed. If using PyPy, be aware of known limitations as Pympler historically has not fully supported it. For specific library objects, check Pympler's changelog for relevant fixes.
TypeError: __sizeof__() takes exactly 1 argument (0 given)
This `TypeError` can arise from compatibility issues between `pympler`'s inspection methods (e.g., `SummaryTracker`) and certain versions of other libraries, such as `pandas`, where `__sizeof__` methods might have incorrect argument signatures.
fixEnsure both `pympler` and any interacting libraries (like `pandas`) are updated to their latest stable versions (`pip install --upgrade pympler pandas`). This often resolves compatibility issues where method signatures have been corrected.
ValueError: invalid option: base=-...
This error occurs within `pympler.asizeof` when an internal calculation or a user-provided option results in a negative value for the 'base' parameter, which is not permitted. This has been observed with certain complex objects or subclasses like `astropy.Table`.
fixReview the objects being sized and any custom options passed to `asizeof`. Update `pympler` to its latest version (`pip install --upgrade pympler`), as fixes for specific object types and internal sizing logic are periodically released. Avoid passing negative values to `asizeof` options like `base`.
Upgrade
Version history
1.1latest on PyPI · released Jun 28, 2024
Audit
Dependencies
pywin32requiredRequired for Pympler's functionality on Windows operating systems.