Suggest an editImprove this articleRefine the answer for “What is an "allocator" in the context of Redis?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)An allocator is a low-level memory manager responsible for allocating, reallocating, and freeing chunks of RAM whenever Redis creates, modifies, or deletes data; by default Redis uses jemalloc instead of standard `malloc`/`free`, since that's inefficient for the huge number of small allocations Redis makes. **Key point:** jemalloc distributes memory across arenas and size classes, which reduces fragmentation, and Redis tracks a fragmentation ratio, `mem_fragmentation_ratio = used_memory_rss / used_memory` - a value above 1.5 means fragmentation is high.Shown above the full answer for quick recall.Answer (EN)ImageAn **allocator** in the context of Redis is a **low-level memory manager** responsible for **allocating, reallocating, and freeing** chunks of RAM whenever Redis creates, modifies, or deletes data. In other words, it's the component that manages **how Redis obtains and uses RAM**. ### 1. Why Redis needs its own allocator Redis is an in-memory database, and it spends almost all of its time working with memory. That's why the standard system `malloc/free` (from the C standard library) turned out to be too slow and inefficient. To reduce **fragmentation** and speed up **small allocations**, Redis uses **specialized external allocators**. ### 2. Which allocators Redis can use Redis can be compiled with different memory-allocator implementations: | Allocator | Description | Used by default | |---|---|---| | jemalloc | fast, efficient, low-fragmentation, supports memory profiling | Yes (the default) | | tcmalloc | from Google, optimized for multi-threading | Sometimes, if set explicitly | | malloc (glibc) | the standard system one, less efficient for Redis | No | The allocator can be chosen when building Redis: ```bash make MALLOC=jemalloc ``` ### 3. How the allocator works inside Redis When Redis creates a new key, value, or internal structure: 1. It asks the allocator to reserve a block of memory of the needed size. 2. The allocator returns a pointer to the allocated region on the heap. 3. Redis writes the data there (a string, a structure, a number, etc.). 4. When the data is deleted, Redis calls `free()`, and the allocator returns the memory to a pool for reuse. The allocator keeps internal records of every allocated block and can redistribute them to reduce fragmentation. ### 4. Why Redis chose **jemalloc** **jemalloc** ("Jason Evans malloc") was designed specifically for systems with a high rate of small allocations. It: - spreads memory across **arenas**, independent regions, which reduces fragmentation; - groups blocks by **size classes**, speeding up reuse; - has built-in statistics (`jemalloc stats`), which Redis exposes via `INFO MEMORY`; - runs stably under heavy load and doesn't "bloat" over time, unlike standard malloc. ### 5. How Redis interacts with the allocator Redis doesn't manage memory directly, it **delegates** allocation to the allocator. But Redis: - tracks the total amount of memory used (`used_memory`); - can trigger cleanup (`MEMORY PURGE`); - can inspect the allocator's state (`MEMORY STATS`, `MEMORY DOCTOR`). ### 6. The fragmentation problem Even with jemalloc, Redis can run into fragmentation, when free memory is scattered in pieces and can't be reused. Redis tracks a fragmentation ratio (`mem_fragmentation_ratio`), computed as: ```javascript mem_fragmentation_ratio = used_memory_rss / used_memory ``` If the value exceeds 1.5, fragmentation is high, and Redis can trigger defragmentation. ### 7. Summary In the context of Redis, an **allocator** is the component that: 1. allocates and frees chunks of RAM for keys, values, and data structures; 2. minimizes fragmentation and overhead; 3. is implemented through the **jemalloc** library (by default); 4. directly affects Redis's performance and stability. The allocator is Redis's "invisible engine", managing every byte of memory inside the process.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.