Guppy 3 (Guppy-PE for Python 3)

3.1.6 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This example demonstrates how to initialize the `heapy` profiler, set a reference point, allocate some objects, and then take a heap snapshot to analyze memory usage. It also shows how to isolate specific objects for inspection.

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()

view raw JSON →