Why a 64-Core Linux Box Made Our Python Service Eat 7.6 GB RAM Every 2 Hours
The Symptom
Across a 200+ node distributed storage management cluster running on 64-core Linux hosts, a core Python REST management service ballooned to 7.6 GB of RSS memory every two hours, triggering thousands of API timeouts before a watchdog restarted it.
Early guesses blamed everything from client request floods to Python object leaks.
Evidence-First Profiling
Rather than guessing, I sampled 370 live thread stacks with py-spy, inspected process virtual memory mappings with pmap, and ran controlled comparisons against a healthy baseline cluster to systematically rule out 9 competing hypotheses.
The evidence pointed to three compounding causes interacting in a nasty feedback loop:
- Unbounded thread pool on 64-core hosts: A default worker formula scaled with CPU count (
CPU × 5), silently spawning 320 persistent worker threads per node. - glibc malloc arena fragmentation: Under Linux glibc, each thread allocates its own malloc arena (up to
8 × cores). Even after Python freed objects, fragmented arenas were never returned to the OS. - 4 MB JSON config re-parsed on every request: To read a single configuration flag, each request loaded and parsed a 4 MB JSON file from scratch—consuming 65% of total CPU.
The Self-Reinforcing GIL Loop
High memory pressure + 320 threads repeatedly parsing 4 MB JSON created fierce Python GIL contention, which starved periodic health-check threads and cascaded into mass REST timeouts.
The Fix
- Config caching: Added an in-memory
mtime-based cache so the 4 MB JSON config is only parsed when the file actually changes. - Bounded concurrency: Replaced the CPU-scaled default with an explicit, bounded worker pool.
- Linux allocator tuning: Configured
MALLOC_ARENA_MAXin the service unit to stop glibc from retaining hundreds of idle memory arenas.
Result
The 2-hour memory runaway and API timeout storms disappeared completely, stabilizing management nodes across the entire 200+ node cluster.
Enjoyed this note or seen a similar bug?
Feel free to drop a line—always glad to exchange ideas with fellow engineers.