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

does malloc use mmap
what system call does malloc use
mmap vs malloc
force to use mmap instead of brk
anonymous mmap vs malloc
how does mmap work
malloc using mmap
mmap memory allocation

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().

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.

"brk()" and "mmap()"?, malloc() uses the data space for "small" allocations and mmap() for "big" ones, for a number of reasons, including reducing memory Also, how does mmap() allocate memory by mapping? So why don't we use mmap()?. 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;

malloc is not limited to using sbrk to allocate memory. It might, for example, use mmap to map a large MAP_ANONYMOUS block of memory; normally mmap will assign a virtual address well away from the data segment.

There are other possibilities, too. In particular, mmap, being a core part of the standard library, is not itself limited to standard library functions; it can make use of operating-system-specific interfaces.

Advanced Memory Allocation, For very large requests, malloc() uses the mmap() system call to find In this case, in fact, had the block been allocated with brk(), it would have remained For example, it can check the size of a memory block passed from  Each arena is a large region of memory that is internally allocated by the system (using brk (2) or mmap (2)), and managed with its own mutexes. The UNIX 98 standard requires malloc (), calloc (), and realloc () to set errno to ENOMEM upon failure.

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 mallocs requirements can be meet from the pre-allocated space, so these mallocs actually did't call brk, it is a optimization in malloc. If next time you want to malloc size beyond that boundary, a new brk will be called (if not large than the mmap threshold).

Does malloc() use brk() or mmap()? https , Does malloc() use brk() or mmap()? requested memory is less than it, brk() will be used; If requested memory is larger than or equals to it, mmap() will be used;  My understanding is that both are used by malloc () to increase the boundary ot heap. brk () is a traditional way of allocating memory in UNIX -- it just expands the data area by a given amount. mmap () allows you to allocate independent regions of memory without being restricted to a single contiguous chunk of virtual address space.

brk and sbrk, brk and sbrk are basic memory management system calls used in Unix and Unix-​like operating systems to control the amount of memory allocated to the data segment of the process. These functions are typically called from a higher-level memory management library function such as malloc. The break value can be automatically rounded up to a size appropriate for the  The malloc() function allocates uninitialized space for an object of the specified size. malloc() maintains multiple lists of free objects according to size, allocating from the appropriate list or requesting memory from the kernel. The allocated space is suitably aligned (after possible pointer coercion) for storage of any type of object.

Chris's Wiki :: blog/unix/SbrkVersusMmap, Unix programs used brk() and sbrk() to create the heap, which is used for dynamic memory allocations via things like malloc() . I know that GNU libc does use brk() based allocation for programs that only make small  Grows the program break, a.k.a. brk, and returns the old program break Effectively, allocates incrementbytes Do not use sbrkin a program that also uses malloc or anything that calls malloc(such as printf) Memory Management with sbrk

Implementing malloc and free - André Carvalho, We can move the program break on our C program by using sbrk() and malloc actually used mmap instead of brk to allocate the memory. The functions malloc, free and friends are not system calls. They are library functions. Under the hood, the system calls they use are mmap and munmap. (Older implementations use brk or brk; I’ll cover that in future.)

Comments
  • #include <unistd.h>?
  • @JS1 Yes, that solved the issue, can you give an explaination, I am new to linux programming...
  • You need the prototype for sbrk() which is in unistd.h. Without a prototype, the compiler assumes that unknown functions return int.
  • @JS1 Yeah, I thought sbrk() was declared in stdlib.h, thank you!
  • @EricWang: It would give a warning if you compiled with -Wall (assuming you're using gcc or clang). You should always compile with -Wall.
  • I found that there is a THRESHOLD to control whether use brk() or mmap(), I updated that in the question.
  • Are you saying that mmap can allocate more memory than sbrk? If so where does sbrk allocate memory, and where does mmap allocate memory? +1 -- good answer.
  • sbrk allocates memory at a specific location; each time you call sbrk you get the a chunk of memory contiguous with the previous call. Historically, this was the frontier between the heap and the stack (with the heap growing up from the data segment and the stack growing down from the end of the process's address space). mmap cannot normally allocate more memory, but it definitely can allocate it with a different address and with a lot more options (memory protection flags, backing store, huge virtual blocks (to reduce page table size), etc.)