2 ms·
It's not as simple as that, because pages are more than one byte long. If pages were one byte long, what you say would be (mostly) true. For every allocation,
by haberman 12y ago
It's not as simple as that, because pages are more than one byte long.
If pages were one byte long, what you say would be (mostly) true. For every allocation, you would just map in some memory at some random place in the address space, and you would map in precisely as many bytes as were requested. You would literally waste no memory, and your reallocs would never need to move memory. Life would be bliss.
But pages are not one byte long. They are usually something like 4k. So if you fulfill a 1-byte malloc() with a 4k page (to leave room for realloc()), you are wasting nearly 4k bytes of real physical memory.
Also: mmap() system calls have CPU overhead, and I highly suspect that two non-contiguous mapped pages create more kernel overhead than two contiguous mapped pages.
Holes are not free.