Who told “malloc” is a system call ?

“The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().”



Recently I was attending a session and the speaker said in between about “malloc” system call. It made this blog..


Ah… Is malloc a system call ? NO.. NO.. NO.. malloc is not a system call.. First of all let me explain what malloc() is.. In simple terms, memory can be assigned either statically or dynamically.. malloc() is a routine which can be used to allocate memory in dynamic way.. But please note that “malloc” is not a system call, it is provided by C library..


The memory can be requested at run time via malloc call and this memory is returned on “heap” ( internal?)  space. Before getting into the details of malloc() call, let me explain the (s)brk() ( “brk” family)  calls.. brk() call can be used to expand/shrink the heap space. More precicely “brk()” call can be used to adjust “data” segment of a program.

malloc() routine make use of brk()  calls to do this job. Library calls should give us more performance than the system call.. brk()  call is executed with a batch of malloc requests . Now, Is it only “brk()”  call is used by malloc routine ? No, there is one more dependent call called “mmap()” . The decision on when to use “brk() and when to use “mmap()”  is taken by C library. That said, some of the parameters of C library decide whether it has to go ahead with the “brk()” or “mmap()” way.


This is documented in man page of malloc.




Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2). When allocating blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using mallopt(3). Allo‐ cations performed using mmap(2) are unaffected by the RLIMIT_DATA resource limit (see getrlimit(2)).



I am sure it is pretty much clear from above. As you can see in the man page, “malloc()”, realloc(), “calloc()”, “free()” all lives in the same family.. 🙂

Now you have to say , Is malloc() a system call ?

One Reply to “Who told “malloc” is a system call ?”

  1. Wow.. That was an interesting read… Really , till I finished reading this article, I believed “malloc()” is a system call..
    Thanks a lot man.. I am bookmarking your site..

Leave a Reply

Your email address will not be published. Required fields are marked *