AboutBlogContact
DevOps & InfrastructureApril 25, 2001 2 min read 134Updated: June 22, 2026

Hunting Memory Leaks: Glibc Malloc Tuning (2001)

AunimedaAunimeda
📋 Table of Contents

Hunting Memory Leaks: Glibc Malloc Tuning

Writing a C daemon that runs for months is a test of character. One small malloc() without a free() and you'll find your process killed by the OOM (Out Of Memory) killer just when traffic peaks. Glibc's allocator is efficient, but it can be opaque. Here's how to peek under the hood and tune it for long-term stability.

The Magic of MALLOC_CHECK_

If you suspect heap corruption (the dreaded "double free" or "buffer overflow"), don't reach for a heavy debugger yet. Use the environment variable MALLOC_CHECK_.

# Set MALLOC_CHECK_ to 2 to abort immediately on error
export MALLOC_CHECK_=2
./my_daemon

When set to 2, glibc uses a slightly slower but more robust allocation algorithm that adds "guards" around your memory blocks. If you corrupt them, the program crashes immediately with a backtrace. It's the fastest way to find that one stray strcpy.

Tuning with mallopt()

Glibc doesn't always return memory to the system immediately. It keeps a "pool" to speed up future allocations. For a daemon, this can look like a leak. You can tune this behavior with mallopt().

#include <malloc.h>

// Disable the "fastbins" to reduce fragmentation
mallopt(M_MXFAST, 0);

// Set the threshold for when to use mmap() instead of sbrk()
// Large allocations are better handled by mmap as they return to OS immediately
mallopt(M_MMAP_THRESHOLD, 128 * 1024);

Inspecting with malloc_stats()

If you want to know what's happening in real-time, call malloc_stats() and pipe it to a log file. It will show you exactly how many bytes are in use, how many are in "free chunks," and how much has been mmap'ed.

malloc_stats();

Remember: free() is just a suggestion to glibc. If you really want to ensure memory is released to the OS, you need to understand the relationship between sbrk and mmap.


Aunimeda provides DevOps engineering and infrastructure services - CI/CD pipelines, containerization, cloud deployments, and monitoring setups.

Contact us to discuss your infrastructure needs. See also: DevOps Services, Custom Software Development

Read Also

Kafka: Zero-Copy and Why It's Fast (2015)aunimeda
DevOps & Infrastructure

Kafka: Zero-Copy and Why It's Fast (2015)

How does Kafka push gigabits of data on commodity hardware? The secret isn't in the code; it's in the Linux kernel's sendfile() call.

The 2008 Scaling Crisis: Caching at the Edge with Memcachedaunimeda
DevOps & Infrastructure

The 2008 Scaling Crisis: Caching at the Edge with Memcached

Your database is the bottleneck. In 2008, if you're hitting your MySQL server for every user profile, you're not scaling. It's time to offload the heavy lifting to a distributed memory pool.

Memcached: Slab Allocation Internals (2007)aunimeda
DevOps & Infrastructure

Memcached: Slab Allocation Internals (2007)

Why is your cache server swapping? It's probably memory fragmentation. Let's look at how Memcached solves this with slabs.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

DevOps Services

Get Consultation All articles