objgraph

3.6.2 · active · verified Sat Apr 11

objgraph is a Python module that lets you visually explore Python object graphs. It helps in debugging, understanding memory usage, and finding memory leaks by drawing object reference graphs using Graphviz. The library is actively maintained with regular updates for Python version compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create some interconnected Python objects and then use `objgraph.show_refs` to visualize objects reachable from a given object, `objgraph.show_backrefs` to see what objects reference a specific object, and `objgraph.show_most_common_types` to get a summary of memory usage. For graph visualization, ensure the Graphviz system utility is installed and accessible in your PATH.

import objgraph

def create_objects():
    x = []
    y = [x, [x], {'x_key': x}]
    z = {'list_ref': y, 'dict_ref': {'inner_x': x}}
    return x, y, z

x, y, z = create_objects()

# Show references reachable from 'y', save to a PNG
objgraph.show_refs([y], filename='sample-graph.png', max_depth=2)
print("Generated sample-graph.png showing references from 'y'")

# Show backreferences to 'x', save to a PNG
objgraph.show_backrefs([x], filename='sample-backref-graph.png', max_depth=2)
print("Generated sample-backref-graph.png showing backreferences to 'x'")

# Show most common types in memory
print("\nMost common types in memory:")
objgraph.show_most_common_types(limit=5)

view raw JSON →