Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Visualizing Object Graphs with objgraph | Profiling and Leak Detection
Python Memory Management

Visualizing Object Graphs with objgraph

Stryg for at vise menuen

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:

123456789101112
import 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:

1234567891011121314
import 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:

12345678910111213141516171819202122
import 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:

12345678910111213
import 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

question mark

What does objgraph.show_growth() report?

Vælg det korrekte svar

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

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:

123456789101112
import 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:

1234567891011121314
import 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:

12345678910111213141516171819202122
import 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:

12345678910111213
import 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
some-alt