Memory Fragmentation

Besides having slight performance problems, malloc() also creates memory fragmentation. How much and how bad that is depends a lot of the program. Usually it matters only with long running processes, short living processes free all their memory soon anyway.

Fragmentation happens when allocating and freeing different sized memory blocks. The malloc() implementation may not find an empty block with exactly the wanted size so it uses a larger block. The unused part may be used for other even smaller allocations, but if it happens a lot there will soon be a lot of these small holes unused.

Programs allocating memory blocks of only few sizes generally don't get fragmented much. Since most programs have traditionally used statically sized buffers for handling most of temporary allocations (string manipulation) they don't get too fragmented.

There are many different malloc implementations. Some concentrate on speed and some on reducing fragmentation, but most try to find a balance on them. Most implementations are portable and quite small, so if you find some of them working much better than others with your program, you could just include it with your program and use it directly.

There's several ways to reduce or even completely eliminate fragmentation by using different allocation methods:

Related Studies

The Memory Fragmentation Problem: Solved?

Are Mallocs Free of Fragmentation?