1

When I try the below code I am not clearly able to analyze malloc api internal calls.What I am not clear is about the system call mmap is called only once for 2 or more malloc calls.If I am assigning more then 4069 bytes also it is calling only one mmap internally(trace is identified by using strace -p processid ).

#include<stdio.h>
#include<stdlib.h>

main()
{
int *p,*q;
sleep(20);
p=malloc(5096);
printf("p=%p\n",p);
q=malloc(4096);
printf("q=%p\n",q);
sleep(2);
return 0;
}

strace OUTPUT:

root@TEST:/home/harish# strace  -p 6109
Process 6109 attached
restart_syscall(<... resuming interrupted call ...>
) = 0
brk(0)                                  = 0xeca000
brk(0xeec000)                           = 0xeec000
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 14), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f10b7bc7000
write(1, "p=0xeca010\n", 11)            = 11
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({20, 0},
0x7ffc34a51790)      = 0
write(1, "q=0xecb020\n", 11)            = 11
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({2, 0}, 0x7ffc34a51790)       = 0
exit_group(0)                           = ?
+++ exited with 0 +++

What I am looking is ,if malloc is used more then once will it call more then one mmap since memory is exceeding in two malloc's beyond 4096

Harish
  • 341
  • 1
  • 13

2 Answers2

2

malloc() does not result into mmap() call. Generally it would result into brk(). However, not each call will result into brk(). It depends a lot on currently allocated pages, asked memory and other things.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • But how the memory is getting allocated is through mmap ,correct me if I am wrong. – Harish Jun 15 '16 at 08:48
  • @harish, That `mmap' might be due to other code like `printf()`. – Rohan Jun 15 '16 at 08:51
  • This may be a follish question but please give me some guidence in understanding like when will brk get called and when will sbrk get called in malloc internals is not clear – Harish Jun 15 '16 at 09:08
  • It's an implementation detail that malloc is deliberately not exposing so you don't have to worry about it. If you _want_ to worry about it, you need to read the implementation. – Useless Jun 15 '16 at 09:21
2

Your process' internal heap (accessed via malloc, free and realloc) manages memory as it sees fit - this includes:

  • growing the heap by large or fixed increments to amortize the cost of expensive brk/sbrk syscalls over multiple (de)allocations
  • dealing with smaller (de)allocations inside that heap area itself
  • managing (de)fragmentation of allocated records

It's also common to use different mechanisms for large and small allocations, for example small objects are allocated from that contiguous area managed by brk/sbrk, but individual large objects may be allocated directly with mmap.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • But after commenting printf in my code I cant see any mmap in trace only brk is displayed twice.So what I am expecting is mmap is not having any relation with malloc – Harish Jun 17 '16 at 04:21
  • I said malloc _may_ use `mmap`, for some allocations, in some implementations. It isn't mandatory. If you want to understand what's happening inside _your_ implementation, read the code. – Useless Jun 17 '16 at 08:39