Diagnosing Memory "Leaks" in Python
pythonmemorydebuggingceleryperformance
Abstraction: Systematic approach to diagnosing Python process memory growth
Key points:
- Django's DEBUG mode stores every query in memory, mimicking a leak — always check this first
- Toolchain:
htopfor observation, GNUtime -vfor peak usage,resourcemodule for inline measurement,objgraphfor object counts,guppy/heapyfor heap diffs - GDB with
gdb-heapcan reveal allocations invisible to Python's GC (e.g., C-level fragmentation) - Python's VM retains peak memory indefinitely even after GC; processes show "high watermark" behavior that is not a true leak — known as memory fragmentation
- Celery-specific fix:
CELERYD_MAX_TASKS_PER_CHILDrolls worker processes; general fix ismultiprocessingisolation viaprocessify - Best solution is using less memory; breaking work into smaller chunks avoids the watermark problem entirely
Connections: Celery · Django · Memory Management · Debugging · Python
Source: http://chase-seibert.github.io/blog/2013/08/03/diagnosing-memory-leaks-python.html