Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Measuring Memory with tracemalloc | Profiling and Leak Detection
Python Memory Management

Measuring Memory with tracemalloc

Swipe to show menu

Before optimizing memory usage, you need to measure it. tracemalloc is Python's built-in memory tracer – it tracks every allocation made by the interpreter and lets you take snapshots, compare them, and pinpoint exactly which line of code is responsible for the most memory.

Basic Usage

tracemalloc.start() begins tracing. tracemalloc.take_snapshot() captures the current allocation state. tracemalloc.stop() ends tracing:

123456789101112131415161718
import tracemalloc tracemalloc.start() # Allocating some data to trace employee_records = [ {"id": emp_id, "name": f"Employee_{emp_id}", "salary": 50000 + emp_id * 100} for emp_id in range(10000) ] snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics("lineno") print("Top 3 memory allocations:") for stat in top_stats[:3]: print(stat) tracemalloc.stop()

Each stat line shows the file, line number, total size, and number of allocations for that line.

Comparing Two Snapshots

The most useful pattern is comparing a snapshot before and after an operation to see exactly what was allocated:

123456789101112131415161718192021222324
import tracemalloc tracemalloc.start() snapshot_before = tracemalloc.take_snapshot() # Operation to measure transaction_log = [] for transaction_id in range(50000): transaction_log.append({ "id": transaction_id, "amount": transaction_id * 12.5, "status": "pending", }) snapshot_after = tracemalloc.take_snapshot() top_stats = snapshot_after.compare_to(snapshot_before, "lineno") print("Memory growth by line:") for stat in top_stats[:5]: print(stat) tracemalloc.stop()

compare_to() shows the delta – only lines where allocations increased.

Finding the Peak

tracemalloc.get_traced_memory() returns the current and peak memory used since tracing started:

12345678910111213141516
import tracemalloc tracemalloc.start() # Allocating and then partially freeing data batch_one = [list(range(1000)) for _ in range(500)] current, peak = tracemalloc.get_traced_memory() print(f"After batch one – current: {current / 1024:.1f} KB, peak: {peak / 1024:.1f} KB") del batch_one batch_two = [list(range(100)) for _ in range(500)] current, peak = tracemalloc.get_traced_memory() print(f"After batch two – current: {current / 1024:.1f} KB, peak: {peak / 1024:.1f} KB") tracemalloc.stop()

Peak memory tells you the worst-case usage during execution – important for sizing containers and servers.

Inspecting Top Allocations with Traceback

Use statistics("lineno") and access traceback[0] to get the file and line responsible for each allocation:

1234567891011121314
import tracemalloc tracemalloc.start() report_data = {f"report_{report_id}": list(range(500)) for report_id in range(1000)} snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics("lineno") for stat in top_stats[:5]: frame = stat.traceback[0] print(f"{frame.filename}:{frame.lineno}{stat.size / 1024:.1f} KB – {stat.count} allocs") tracemalloc.stop()
question mark

What does snapshot_after.compare_to(snapshot_before, "lineno") show?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 3. Chapter 1
some-alt