Guppy 3 (Guppy-PE for Python 3)
Guppy 3 is a comprehensive Python programming environment and heap analysis toolset, primarily serving as a port of the original Guppy-PE (Guppy-Python Environment) from Python 2 to Python 3. It provides robust tools for object and heap memory sizing, profiling, and analysis, essential for debugging memory-related issues and optimizing Python program performance. The library consists of subpackages like `heapy` for detailed heap introspection, `gsl` for a specification language, and `sets` for C-implemented bitsets and nodesets. The project maintains an active development status with regular minor releases.
Warnings
- gotcha Python Version Incompatibility: Guppy3 currently requires Python 3.10-3.14. Using it with unsupported Python versions (e.g., older 3.x versions like 3.9 or earlier, or Python 2) will lead to installation failures or runtime errors.
- gotcha CPython Exclusive & Free-threaded CPython Not Supported: Guppy3 is strictly for CPython and does not work with other Python implementations (like PyPy). Additionally, it currently lacks support for future free-threaded CPython versions due to internal complexities.
- gotcha Tkinter Dependency for Graphical Browser: The interactive graphical heap profiler requires the `Tkinter` library, which is not always included with Python installations. Attempting to use GUI features without it will fail.
- gotcha Potential TensorFlow Incompatibility: Users have reported that `h.heap()` calls might terminate without explicit errors when used within applications that extensively use TensorFlow, suggesting possible underlying conflicts.
- gotcha Historical Windows Crash with pywin32: Older `guppy3` versions on Windows could crash if `pywin32` was below version 300. While likely resolved with current `pywin32` releases, this could still be a factor in older or specific Windows setups.
Install
-
pip install guppy3 -
conda install -c conda-forge guppy3
Imports
- hpy
from guppy import hpy
Quickstart
from guppy import hpy
def create_some_objects():
a = [1, 2, 3] * 1000
b = {'x': 1, 'y': 2} * 500
return a, b
h = hpy() # Create a Heapy session context
h.setref() # Set a reference point for memory tracking
# Perform some operations that allocate memory
obj_a, obj_b = create_some_objects()
# Get a heap snapshot of newly allocated objects since setref()
heap_snapshot = h.heap()
print("Memory usage snapshot:")
print(heap_snapshot)
# To view the shortest paths to the single largest object (if any):
# if heap_snapshot.byid:
# print("\nShortest path to largest object:")
# print(heap_snapshot.byid[0].sp)
# Example of isolating objects
h_iso = h.iso(obj_a, obj_b)
print("\nIsolated objects snapshot:")
print(h_iso)
# You can also run internal tests to verify installation:
# h.test()