2 min read

Python Tracemalloc Module

The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides detailed information about memory allocation, allowing you to find memory leaks and understand memory usage.

Importing the Module

import tracemalloc

Basic Usage

To start tracing memory allocations, use tracemalloc.start().

import tracemalloc

tracemalloc.start()

# ... run your application ...

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')

print("[ Top 10 ]")
for stat in top_stats[:10]:
    print(stat)

Displaying the Top 10

You can display the files and lines that are allocating the most memory.

import tracemalloc

tracemalloc.start()

# Allocate some memory
my_list = [x for x in range(100000)]
my_dict = {x: x for x in range(100000)}

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')

print("[ Top 10 ]")
for stat in top_stats[:10]:
    print(stat)

Comparing Snapshots

To detect memory leaks, you can compare two snapshots taken at different times.

import tracemalloc

tracemalloc.start()

# Take first snapshot
snapshot1 = tracemalloc.take_snapshot()

# Allocate memory
x = [i for i in range(10000)]

# Take second snapshot
snapshot2 = tracemalloc.take_snapshot()

top_stats = snapshot2.compare_to(snapshot1, 'lineno')

print("[ Top 10 differences ]")
for stat in top_stats[:10]:
    print(stat)

Getting the Traceback

You can get the traceback of a memory block to see exactly where it was allocated. You need to set the number of frames to store when starting.

import tracemalloc

# Store 25 frames
tracemalloc.start(25)

# ... code ...
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('traceback')

# Pick the biggest memory block
stat = top_stats[0]
print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB")
for line in stat.traceback.format():
    print(line)

programming/python/python