{"id":1868,"library":"pympler","title":"Pympler","description":"Pympler is a development tool designed to measure, monitor, and analyze the memory behavior of Python objects in a running application. It provides detailed insights into the size and lifetime of Python objects, aiding in the identification of memory bloat, leaks, and other undesirable runtime behaviors. The current stable version is 1.1.","status":"active","version":"1.1","language":"en","source_language":"en","source_url":"https://github.com/pympler/pympler","tags":["memory profiling","profiling","memory analysis","debugging","object sizing"],"install":[{"cmd":"pip install Pympler","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for Pympler's functionality on Windows operating systems.","package":"pywin32","optional":false}],"imports":[{"note":"For sizing individual Python objects recursively.","symbol":"asizeof","correct":"from pympler import asizeof"},{"note":"For on-line monitoring of all objects in a Python application.","symbol":"muppy","correct":"from pympler import muppy"},{"note":"To format and print summaries of object lists obtained from muppy or classtracker.","symbol":"summary","correct":"from pympler import summary"},{"note":"For tracking changes in overall memory or specific class instances over time.","symbol":"tracker","correct":"from pympler import tracker"}],"quickstart":{"code":"from pympler import asizeof, muppy, summary\n\nclass MyObject:\n    def __init__(self, data):\n        self.data = data\n        self.large_list = [0] * 1000 # A list consuming some memory\n\n# 1. Measure the size of an individual object recursively\nobj = MyObject('some string data')\nprint(f\"Size of obj: {asizeof.asizeof(obj)} bytes\")\n\n# 2. Get a summary of all objects in memory\nall_objects_before = muppy.get_objects()\nsum1 = summary.summarize(all_objects_before)\nprint(\"\\n--- Memory Summary (Before) ---\")\nsummary.print_(sum1, limit=5) # Print top 5 largest object types\n\n# Create more objects\nmy_list = [MyObject(f'data_{i}') for i in range(10)]\n\n# 3. Get a diff in memory usage\nall_objects_after = muppy.get_objects()\nsum2 = summary.summarize(all_objects_after)\nprint(\"\\n--- Memory Summary (After, diff) ---\")\ndiff = summary.diff(sum1, sum2)\nsummary.print_(diff, limit=5) # Print top 5 changes\n\n# Example of tracker for change over time (simplified)\ntr = tracker.SummaryTracker()\n\n# Perform some operations that might create new objects\ndef create_temp_objects():\n    local_list = [\"temp_str\"] * 500\n\ncreate_temp_objects()\n\nprint(\"\\n--- Tracker Diff after create_temp_objects() ---\")\ntr.print_diff(limit=5)\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade to Python 3.6+ or use an older Pympler version (e.g., `pip install 'Pympler<1.0'` for Python 2.7/3.5).","message":"Pympler dropped support for Python 2.7 and Python 3.5 starting with version 1.0. Older Python versions require Pympler < 1.0.","severity":"breaking","affected_versions":"1.0 and later"},{"fix":"Use `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.","message":"Repeated 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be 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.","message":"The `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.","severity":"gotcha","affected_versions":"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."},{"fix":"Use 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.","message":"Pympler 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.","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"}