Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does malloc() use brk() or mmap()?

c code:

// program break mechanism // TLPI exercise 7-1  #include <stdio.h> #include <stdlib.h>  void program_break_test() {     printf("%10p\n", sbrk(0));      char *bl = malloc(1024 * 1024);     printf("%x\n", sbrk(0));      free(bl);     printf("%x\n", sbrk(0));  }  int main(int argc, char **argv) {     program_break_test();     return 0; } 

When compiling following code:

 printf("%10p\n", sbrk(0)); 

I get warning tip:

format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’

Question 1: Why is that?


And after I malloc(1024 * 1024), it seems the program break didn't change.

Here is the output:

9b12000 9b12000 9b12000 

Question 2: Does the process allocate memory on heap when start for future use? Or the compiler change the time point to allocate? Otherwise, why?


[update] Summary: brk() or mmap()

After reviewing TLPI and check man page (with help from author of TLPI), now I understand how malloc() decide to use brk() or mmap(), as following:

mallopt() could set parameters to control behavior of malloc(), and there is a parameter named M_MMAP_THRESHOLD, in general:

  • If requested memory is less than it, brk() will be used;
  • If requested memory is larger than or equals to it, mmap() will be used;

The default value of the parameter is 128kb (on my system), but in my testing program I used 1Mb, so mmap() was chosen, when I changed requested memory to 32kb, I saw brk() would be used.

The book mentioned that in TLPI page 147 and 1035, but I didn't read carefully of that part.

Detailed info of the parameter could be found in man page for mallopt().

like image 517
user218867 Avatar asked May 30 '15 04:05

user218867


People also ask

Does malloc use brk or mmap?

If you use malloc in your code, it will call brk() at the beginning, allocated 0x21000 bytes from the heap, that's the address you printed, so the Question 1: the following malloc s requirements can be meet from the pre-allocated space, so these mallocs actually didn't call brk , it is a optimization in malloc .

Does malloc always call mmap?

For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space.

What is the difference between malloc and mmap?

Malloc generally functions in most of the memory management process. In the event the program requires additional memory, this is borrowed from the OS. Mmap on the other hand makes use of a context switch that converts into kernel land.

What algorithm does malloc use?

Malloc Algorithm In a nutshell, malloc works like this: If there is a suitable (exact match only) chunk in the tcache, it is returned to the caller. No attempt is made to use an available chunk from a larger-sized bin. If the request is large enough, mmap() is used to request memory directly from the operating system.


1 Answers

If we change the program to see where the malloc'd memory is:

#include <unistd.h> #include <stdio.h> #include <stdlib.h>  void program_break_test() {   printf("%10p\n", sbrk(0));    char *bl = malloc(1024 * 1024);   printf("%10p\n", sbrk(0));   printf("malloc'd at: %10p\n", bl);    free(bl);   printf("%10p\n", sbrk(0));  }  int main(int argc, char **argv) {   program_break_test();   return 0; } 

It's perhaps a bit clearer that sbrk wouldn't change. The memory given to us by malloc is being mapped into a wildly different location.

You could also use strace on Linux to see what system calls are made, and find out that malloc is using mmap to perform the allocation.

like image 146
Jay Kominek Avatar answered Sep 28 '22 09:09

Jay Kominek