Draws Python object reference graphs with graphviz
npx @tessl/cli install tessl/pypi-objgraph@3.6.0A Python module for visualizing Python object reference graphs with graphviz. objgraph helps developers track memory usage, identify memory leaks, and understand object relationships by providing tools to count objects, analyze growth patterns, and generate visual graphs of object references.
pip install objgraphimport objgraphimport objgraph
# Count objects by type
print(objgraph.count('list')) # Count all list objects
print(objgraph.count('dict')) # Count all dict objects
# Show most common object types
objgraph.show_most_common_types()
# Track object growth between code sections
objgraph.show_growth()
# ... run some code that might create objects ...
objgraph.show_growth() # Shows increases since last call
# Find objects by type
my_lists = objgraph.by_type('list')
print(f"Found {len(my_lists)} list objects")
# Visualize what refers to an object
my_object = [1, 2, 3]
objgraph.show_backrefs([my_object], filename='backrefs.png')
# Visualize what an object refers to
objgraph.show_refs([my_object], filename='refs.png')objgraph operates on Python's garbage collector and provides three main interaction patterns:
The module integrates with Python's gc module to access tracked objects and uses graphviz for visualization. Optional xdot integration provides interactive graph viewing.
Functions for counting objects, analyzing type distributions, and getting statistical overviews of memory usage patterns.
def count(typename, objects=None): ...
def typestats(objects=None, shortnames=True, filter=None): ...
def most_common_types(limit=10, objects=None, shortnames=True, filter=None): ...
def show_most_common_types(limit=10, objects=None, shortnames=True, file=None, filter=None): ...Tools for tracking object creation and growth patterns over time, essential for identifying memory leaks and monitoring memory usage.
def growth(limit=10, peak_stats={}, shortnames=True, filter=None): ...
def show_growth(limit=10, peak_stats=None, shortnames=True, file=None, filter=None): ...
def get_new_ids(skip_update=False, limit=10, sortby='deltas', shortnames=None, file=None, _state={}): ...Functions for finding specific objects by type or memory address, and identifying potential memory leaks.
def by_type(typename, objects=None): ...
def at(addr): ...
def at_addrs(address_set): ...
def get_leaking_objects(objects=None): ...Tools for tracing reference chains between objects to understand how objects are connected and what keeps them alive.
def find_ref_chain(obj, predicate, max_depth=20, extra_ignore=()): ...
def find_backref_chain(obj, predicate, max_depth=20, extra_ignore=()): ...Generate visual representations of object reference graphs using graphviz, with support for interactive viewing and various output formats.
def show_backrefs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10, highlight=None, filename=None, extra_info=None, refcounts=False, shortnames=True, output=None, extra_node_attrs=None): ...
def show_refs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10, highlight=None, filename=None, extra_info=None, refcounts=False, shortnames=True, output=None, extra_node_attrs=None): ...
def show_chain(*chains, **kw): ...def is_proper_module(obj):
"""
Check if object is a proper module (in sys.modules).
Args:
obj: Object to check
Returns:
bool: True if obj is a proper module
"""# Module metadata
__version__: str # "3.6.2"
__author__: str # "Marius Gedminas (marius@gedmin.as)"
__copyright__: str # "Copyright (c) 2008-2023 Marius Gedminas and contributors"
__license__: str # "MIT"
__date__: str # "2024-10-10"
# Runtime configuration
IS_INTERACTIVE: bool # True if running in IPython/Jupyterobjgraph functions generally raise standard Python exceptions:
ValueError: For invalid parameter combinations (e.g., specifying both filename and output in visualization functions)AttributeError: For missing object attributes during introspectionOSError/FileNotFoundError: When output files cannot be createdImportError: When optional dependencies like graphviz are missingException and return safe string alternatives