debugpythonMajorpending
Debug: Python memory leaks with tracemalloc
Viewed 0 times
memory leaktracemallocobjgraphgarbage collectionweakref
Error Messages
Problem
Python application's memory usage grows over time, eventually causing OOM or degraded performance.
Solution
Use tracemalloc to find memory leaks:
Common causes:
Quick check:
import tracemalloc
import linecache
# Start tracing
tracemalloc.start()
# ... run your application ...
# Take a snapshot
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
print('Top 10 memory allocations:')
for stat in top_stats[:10]:
print(stat)
# Compare two snapshots to find growth
tracemalloc.start()
snapshot1 = tracemalloc.take_snapshot()
# ... run more of your application ...
snapshot2 = tracemalloc.take_snapshot()
stats = snapshot2.compare_to(snapshot1, 'lineno')
print('Top memory growth:')
for stat in stats[:10]:
print(stat)Common causes:
# 1. Growing lists/dicts (caches without eviction)
class Service:
def __init__(self):
self._cache = {} # Grows forever!
# Fix: use lru_cache or TTL cache
# 2. Unclosed resources
def process():
f = open('file.txt') # Never closed!
# Fix: use 'with' statement
# 3. Circular references with custom destructors
class Node:
def __init__(self):
self.parent = None
self.children = []
# Circular refs with __del__ can't be GC'd
# Fix: use weakref for back-references
import weakref
class Node:
def __init__(self):
self._parent = None # weakref
self.children = []
@property
def parent(self):
return self._parent() if self._parent else None
# 4. Event handlers not removed
# Fix: use weak references or explicit cleanupQuick check:
import objgraph; objgraph.show_growth(limit=10)Why
Python's garbage collector handles most cases, but circular references, caches, and unclosed resources can cause steady memory growth.
Context
Long-running Python applications with growing memory usage
Revisions (0)
No revisions yet.