Introduction
Memory leaks in Python can silently degrade performance and even crash services in production. This guide walks you through diagnosing, fixing, and preventing leaks with real‑world examples.
Detecting Leaks
Using tracemalloc
import tracemalloc
tracemalloc.start()
# ... run workload ...
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:10]:
print(stat)
tracemalloc shows which lines allocate the most memory over time.
Leveraging objgraph
import objgraph
objgraph.show_most_common_types(limit=5)
objgraph.show_backrefs(objgraph.by_type('list')[0], max_depth=3)
Visualize object graphs to spot unexpected growth.
Common Culprits
- Global containers (lists, dicts) that grow without bounds.
- Unclosed file/network handles.
- C extensions leaking native memory.
-
Reference cycles involving
__del__methods.
Fixing the Leak
Example: Cleaning a Global Cache
# Bad pattern – cache never cleared
cache = {}
def get_item(key):
return cache.setdefault(key, load_from_db(key))
Fixed version
cache = {}
CACHE_MAX_SIZE = 10_000
def get_item(key):
if key not in cache:
if len(cache) >= CACHE_MAX_SIZE:
# Simple LRU eviction
cache.pop(next(iter(cache)))
cache[key] = load_from_db(key)
return cache[key]
def clear_cache():
cache.clear()
The eviction logic caps memory usage.
Closing Resources
with open('data.txt') as f:
data = f.read()
# file automatically closed
Avoid manual close() calls that can be missed in exception paths.
Preventive Measures
-
Run
tracemallocin CI – fail builds if memory growth exceeds a threshold. -
Enable
faulthandlerto get detailed dumps on crashes. -
Use
resourcelimits in containers (--memoryflag) to catch runaway processes early.
Deploy‑time Checks
Add a health‑check endpoint that reports current RSS:
import psutil, os
def memory_health():
mem = psutil.Process(os.getpid()).memory_info().rss
return {'rss_bytes': mem}
Alert when the value crosses a safe limit.
Conclusion
By combining runtime diagnostics, disciplined coding patterns, and automated checks, you can eradicate Python memory leaks before they impact users. Ready to patch your services? Download the pre‑configured script here. Or Get the complete patch tool. For the full codebase, Access the full repository fix.
Top comments (0)