Linux OS Internals (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Linux Memory Allocators: Glibc `malloc`, `jemalloc`, and `tcmalloc` in high-concurrency applications.
senior Levellinux-osAdvanced Internals
In Simple Words:
Standard glibc malloc uses global mutex locks or a limited number of memory arenas. In applications with 64+ concurrent threads (e.g. MySQL, Redis, Java apps), threads spend excessive CPU time waiting on lock contention during memory allocations.
jemalloc(FreeBSD / Meta): Partitions memory into thread-specific arenas and size-classes. Dramatically reduces memory fragmentation and eliminates lock contention.tcmalloc(Google Thread-Caching Malloc): Thread-local memory caches for small allocations (< 32 KB), making multi-threaded allocation near zero-overhead.