Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Confirm the growth with metrics, compare tracemalloc snapshots over time to find allocation sites that keep growing, inspect what holds those objects with tools like objgraph or gc.get_referrers, and fix the retaining reference, often an unbounded cache, global list or leaked task.
First check whether memory is actually growing without bound or simply plateauing because the allocator keeps freed memory for reuse. tracemalloc can take snapshots at intervals and compare them by file and line, revealing where retained memory was allocated. Tools such as objgraph show which object types grow and what references keep them alive, and memray gives detailed allocation reports, including native allocations from C extensions. Common causes are module-level caches or lists that grow forever, lru_cache on methods, pending asyncio tasks that never finish, reference cycles with objects that hold large buffers, and leaks inside native libraries. Fix the owner, bound caches, and as a safety net, recycle workers after a number of tasks, as Celery and Gunicorn support.