Which of these calls is faster on average? I've heard that mmap
is faster for smaller allocations but I haven't heard a comparison of either. Any information on performance for these would be nice.
Asked
Active
Viewed 6,586 times
5

Guy Avraham
- 3,482
- 3
- 38
- 50

Jesus Ramos
- 22,940
- 10
- 58
- 88
-
You are aware that they do similar but different things ? – DarkDust Apr 01 '11 at 18:55
-
1Yes I do. I just wanted to know if replacing one for the other in acceptable circumstances would lead to a performance difference. – Jesus Ramos Apr 01 '11 at 18:57
-
1Your mileage may vary. Best to measure against the exact environment you care about. – bmargulies Apr 01 '11 at 19:08
-
1Note: if you call sbrk you will risk breaking most malloc implmentations. This has consequences. For example, common C library calls [ like strdup() ], can be affected because they employ malloc(). – jim mcnamara Apr 01 '11 at 19:33
-
1Indeed, you definitely **cannot** use `sbrk` (or worse yet `brk`) in a program that might call `malloc`. And since any standard library function could call `malloc`, that means you cannot use the standard library, period. – R.. GitHub STOP HELPING ICE Apr 01 '11 at 19:41
-
I was writing a memory allocator so that isnt a problem since my allocator will be linked in place of malloc. I was just wondering about performance since it will be internally used for certain specific usages. – Jesus Ramos Apr 01 '11 at 19:49
1 Answers
12
You should tag this with a particular implementation (like linux
) since the answer surely varies by implementation. For now I'll assume Linux since it's the most popular.
With that said, brk
is in theory more optimizable, and in practice it runs about 10% faster on my machine. Allocating one page, these are the times I get:
brk
: min 2550 cycles, typical 2650 cyclesmmap
: min 2700 cycles, typical 2800 cycles
I remember hearing something along the lines of brk
being able to skip locking the mmap
semaphore, which would explain the discrepancy.
Note: I updated these times after adjusting my test to make a dummy calls prior to timing, to ensure that the code would all be in the cache.

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
-
oddly enough for me mmap was actually running faster than sbrk but then again with sbrk its possible to keep memory contiguous. – Jesus Ramos Apr 04 '11 at 16:24
-
1Perhaps your system's `sbrk` is performing some userspace locking or accounting, or even calling the `brk` syscall twice. (once to get the old `brk` and again to set the new one...?) If you're implementing `malloc` I would not rely on the system library's `sbrk` but make the `brk` syscall yourself. – R.. GitHub STOP HELPING ICE Apr 04 '11 at 16:42
-
I ended up using mmap as the primary way to grab memory from the system and use sbrk as a backup in case I get MAP_FAILED, I might try using the brk call instead to see what the performance of that would be. – Jesus Ramos Apr 04 '11 at 16:48