Advertisement
Advanced Python, Debugging & Profiling

How would you profile memory allocations specifically (not just CPU time) to find which objects are consuming the most memory in a running process?

Short answer

Combine tracemalloc's snapshot statistics grouped by 'traceback' (not just 'lineno') to see the FULL call stack responsible for each allocation, or use a dedicated tool like objgraph to visualize reference chains keeping specific objects alive — CPU profilers like cProfile don't…

Save Python mock interview
Advertisement
AModel answer
Combine tracemalloc's snapshot statistics grouped by 'traceback' (not just 'lineno') to see the FULL call stack responsible for each allocation, or use a dedicated tool like objgraph to visualize reference chains keeping specific objects alive — CPU profilers like cProfile don't measure memory at all, so a dedicated memory-focused tool is required.
import tracemalloc

tracemalloc.start(25)  # capture up to 25 frames of traceback per allocation
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics("traceback")
for stat in top_stats[:3]:
    print(stat)
    for line in stat.traceback.format():
        print(line)
Real-world example
Diagnosing exactly which code path is responsible for the largest chunk of memory usage in a long-running data processing service.
Likely follow-ups
  • How does objgraph's show_backrefs() help find WHY a specific object is still being kept alive by the garbage collector?
Related topics