Introduction
Memory leaks in long‑running Python services can silently eat RAM, cause GC thrashing, and eventually crash your production pods. This guide walks you through diagnosing, fixing, and preventing leaks with real‑world examples.
1. How Python Allocates Memory
- CPython heap vs. OS heap
- Reference counting + cyclic GC
- When objects survive beyond their useful life
2. Typical Leak Patterns
# Example: accumulating objects in a global list
leaked = []
def handle_request(data):
obj = process(data)
leaked.append(obj) # <-- forgetting to clear
- Unbounded caches (
functools.lru_cachewithmaxsize=None) - Forgotten file/connection handles
- C extensions that bypass GC
3. Step‑by‑Step Troubleshooting
- Reproduce the leak locally – Run the service with a controlled workload and monitor RSS.
-
Instrument with
tracemalloc– Capture snapshots before and after the workload. - Identify the culprit – Compare snapshots to see which object types grew.
-
Apply a fix – Refactor the code, add explicit
close(), or limit cache size. - Validate – Re‑run the load test and confirm stable memory.
3.1 Using tracemalloc
import tracemalloc, time
tracemalloc.start()
# ... run workload ...
time.sleep(10)
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
print("[Top 10 memory consumers]")
for stat in top_stats[:10]:
print(stat)
3.2 Leveraging objgraph for reference cycles
import objgraph, gc
gc.collect()
objgraph.show_most_common_types(limit=5)
objgraph.show_backrefs([leaked[0]], filename='leak.png')
4. Production‑Ready Monitoring
| Tool | What it Shows |
|---|---|
prometheus + process_resident_memory_bytes
|
RSS over time |
py-spy |
Live stack‑trace sampling |
cProfile + memory_profiler
|
Per‑function memory usage |
Set up alerts when RSS grows > 10 % per hour.
5. Preventive Coding Practices
- Use context managers (
with open(...) as f:) - Limit cache sizes (
functools.lru_cache(maxsize=1024)) - Prefer weak references for large objects (
weakref.WeakValueDictionary) - Close C extension resources explicitly
6. Automated Fix Script
We’ve packaged a ready‑to‑run script that scans your codebase for the most common leak signatures and injects safe‑guards. Download the pre‑configured script here: https://gaba-101010.github.io/GG/
If you need a broader solution, Get the complete patch tool from the same repository, or Access the full repository fix for deeper integration.
Conclusion
Fixing memory leaks is a mix of disciplined coding, proper observability, and quick iteration. By following the steps above you can turn a flaky production service into a stable, resource‑friendly system.
Top comments (0)