Visualizing Object Graphs with objgraph
Svep för att visa menyn
tracemalloc and memory_profiler show how much memory is used. objgraph shows what is in memory – how many instances of each type exist, what references what, and where reference cycles are hiding. It is the go-to tool for diagnosing memory leaks.
Installation
pip install objgraph
Counting Objects by Type
objgraph.show_most_common_types() lists the types with the most live instances:
123456789101112import objgraph # Creating some objects and checking what's in memory class Invoice: def __init__(self, invoice_id, amount): self.invoice_id = invoice_id self.amount = amount invoices = [Invoice(invoice_id, invoice_id * 250.0) for invoice_id in range(1000)] cached_results = {"q1": list(range(500)), "q2": list(range(500))} objgraph.show_most_common_types(limit=10)
This gives an instant overview of what is consuming memory.
Detecting Growth
objgraph.show_growth() compares the current object counts to the previous call and shows only what grew:
1234567891011121314import objgraph class ReportSection: def __init__(self, section_id): self.section_id = section_id objgraph.show_growth() # Baseline snapshot # Simulating a function that leaks objects leaked_sections = [] for section_id in range(500): leaked_sections.append(ReportSection(section_id)) objgraph.show_growth() # Shows what grew since the baseline
If show_growth() keeps showing the same type growing on every call, that type is leaking.
Finding Reference Cycles
objgraph.find_backref_chain() traces the reference path from a specific object back to a root – showing exactly what is keeping it alive:
12345678910111213141516171819202122import objgraph import gc class Node: def __init__(self, label): self.label = label self.child = None # Creating a cycle node_a = Node("parent") node_b = Node("child") node_a.child = node_b node_b.child = node_a # Cycle # Finding what keeps node_a alive chain = objgraph.find_backref_chain( node_a, objgraph.is_proper_module ) print(chain) gc.collect() # Collecting the cycle after inspection
Tracking a Specific Type
objgraph.by_type() returns all live instances of a given type – useful for checking that a factory or cache is not accumulating objects:
12345678910111213import objgraph class DataChunk: def __init__(self, chunk_id): self.chunk_id = chunk_id chunks = [DataChunk(chunk_id) for chunk_id in range(5)] live_chunks = objgraph.by_type("DataChunk") print(f"Live DataChunk instances: {len(live_chunks)}") for chunk in live_chunks: print(f" chunk_id={chunk.chunk_id}")
objgraph vs tracemalloc
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Visualizing Object Graphs with objgraph
tracemalloc and memory_profiler show how much memory is used. objgraph shows what is in memory – how many instances of each type exist, what references what, and where reference cycles are hiding. It is the go-to tool for diagnosing memory leaks.
Installation
pip install objgraph
Counting Objects by Type
objgraph.show_most_common_types() lists the types with the most live instances:
123456789101112import objgraph # Creating some objects and checking what's in memory class Invoice: def __init__(self, invoice_id, amount): self.invoice_id = invoice_id self.amount = amount invoices = [Invoice(invoice_id, invoice_id * 250.0) for invoice_id in range(1000)] cached_results = {"q1": list(range(500)), "q2": list(range(500))} objgraph.show_most_common_types(limit=10)
This gives an instant overview of what is consuming memory.
Detecting Growth
objgraph.show_growth() compares the current object counts to the previous call and shows only what grew:
1234567891011121314import objgraph class ReportSection: def __init__(self, section_id): self.section_id = section_id objgraph.show_growth() # Baseline snapshot # Simulating a function that leaks objects leaked_sections = [] for section_id in range(500): leaked_sections.append(ReportSection(section_id)) objgraph.show_growth() # Shows what grew since the baseline
If show_growth() keeps showing the same type growing on every call, that type is leaking.
Finding Reference Cycles
objgraph.find_backref_chain() traces the reference path from a specific object back to a root – showing exactly what is keeping it alive:
12345678910111213141516171819202122import objgraph import gc class Node: def __init__(self, label): self.label = label self.child = None # Creating a cycle node_a = Node("parent") node_b = Node("child") node_a.child = node_b node_b.child = node_a # Cycle # Finding what keeps node_a alive chain = objgraph.find_backref_chain( node_a, objgraph.is_proper_module ) print(chain) gc.collect() # Collecting the cycle after inspection
Tracking a Specific Type
objgraph.by_type() returns all live instances of a given type – useful for checking that a factory or cache is not accumulating objects:
12345678910111213import objgraph class DataChunk: def __init__(self, chunk_id): self.chunk_id = chunk_id chunks = [DataChunk(chunk_id) for chunk_id in range(5)] live_chunks = objgraph.by_type("DataChunk") print(f"Live DataChunk instances: {len(live_chunks)}") for chunk in live_chunks: print(f" chunk_id={chunk.chunk_id}")
objgraph vs tracemalloc
Tack för dina kommentarer!