iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🧠

What is jemalloc? A Quick Guide to How It Differs from Standard malloc

に公開

What sparked this

I was browsing Hacker News this morning when I saw an article titled "Jemalloc un-abandoned by Meta" trending. I found myself thinking, "Wait, what is jemalloc?"

I looked into it, and it turned out to be surprisingly interesting, so I've decided to write a post about it!


What is malloc, anyway?

If you've touched the C programming language even a little, you've probably seen malloc().

int *arr = (int *)malloc(sizeof(int) * 10);

This is a function that dynamically allocates memory from the "heap" area while a program is running.

  • malloc(size) → Allocate memory
  • free(ptr) → Free memory

Think of it as requesting a large block of memory from the OS and then dividing it up to distribute to the program.


Weaknesses of the standard malloc

The malloc included in the C standard library (the glibc implementation) is general-purpose and stable, but it can have weaknesses in large-scale, high-load server applications.

  1. Fragmentation: Repeated allocation and deallocation can leave the memory sparse (fragmented).
  2. Multi-threaded performance: When multiple threads attempt to allocate memory simultaneously, lock contention can easily occur.

What is jemalloc?

A general purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support.

jemalloc is an alternative implementation of malloc created to solve these problems.

It was originally developed by Jason Evans in 2005 as the standard malloc for FreeBSD. Later, Meta (then Facebook) adopted it for their large-scale services and has been customizing and maintaining it ever since.

Features

Feature glibc malloc jemalloc
Fragmentation defense Standard Strong (Arena management)
Multi-threaded performance Prone to lock contention Reduces contention via arena splitting
Memory usage visualization None Has statistics and profiling functions
Debugging functions Minimal Abundant

What are Arenas?

The key to jemalloc is the concept of arenas.

In a standard malloc, all threads share a single large memory pool, which makes lock contention likely to occur.

jemalloc divides the memory pool into multiple arenas and assigns threads to each arena. This significantly reduces contention.

Thread 1 → Arena 0
Thread 2 → Arena 1
Thread 3 → Arena 0
Thread 4 → Arena 1

Where is it actually used?

  • FreeBSD standard malloc
  • Meta (Facebook) services in general
  • Firefox memory allocator
  • Redis (optional)
  • Rust in some environments

In particular, Meta has reported that adopting jemalloc in their servers, which handle millions of concurrent connections, has enabled them to reduce memory usage by dozens of percent.


What is this "un-abandoned" news?

Maintenance of jemalloc had been stagnant for several years, but the recent news is that in March 2026, Meta declared that they would "start maintaining it properly again."

It is a positive sign for the entire ecosystem when a library that serves as a foundation for infrastructure becomes active again.


Summary

  • malloc is a function responsible for dynamic memory allocation.
  • Standard malloc (glibc) has challenges with fragmentation and multi-threaded performance.
  • jemalloc is an alternative implementation created to solve those issues.
  • It reduces thread contention through arena partitioning, making it highly effective for large-scale services.
  • It is widely adopted by FreeBSD, Meta, Firefox, and more.

Even for someone like me who doesn't write C very often, it was a great learning experience to realize that "memory is something you have to manage manually..." While I still have a lot to learn, next I want to try using jemalloc with LD_PRELOAD! 💪


Reference: https://engineering.fb.com/2026/03/02/data-infrastructure/investing-in-infrastructure-metas-renewed-commitment-to-jemalloc/

GitHubで編集を提案

Discussion