I'm trying to trace down a memory allocation problem I have in OSX. If I compile and run the following code normally, it will run pretty fast.
#include <sys/mman.h>
#define SIZE 8 * 1024 * 1024
int main(int argc, char const *argv[]) {
for (int i = 0; i < 50000; ++i) {
mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
}
return 0;
}
However, if I compile the same code but linking to some library (i.e.: clang -o test test.c -lpcre
) it will randomly either run fast (30ms) or really slow (18 seconds).
Notice that I'm not even using the library, just linking. I also noticed that this seems not to happen with any library.
I'm running OSX 10.10.3. Any ideas?
This is just a bug in the kernel that others have encountered, too. The code in the kernel to find an unused chunk of address space to allocate uses an inefficient search algorithm.
I suspect the reason it seems to depend on whether you link a library is that the dynamic loader (dyld) has to map such a library and that sometimes gets the kernel's VM housekeeping data into the state that triggers the search inefficiency. The reason it doesn't happen on every run probably has to do with Address Space Layout Randomization (ASLR).
I encourage you to file a bug with Apple about this, especially since you have a nice simple test case. (It will probably get closed as a duplicate, since I'm sure you won't be the first person to file it. Still, every new bug report can help isolate it and raises its priority within Apple.)