So malloc does not cause any syscall?

Associated Code:

write(-1, "test", sizeof("test")); void * p = malloc(1024); void * p2 = malloc(510); write(-1, "hi", sizeof("hi")); 

Corresponding strace output:

 write(4294967295, "test\0", 5) = -1 EBADF (Bad file descriptor) brk(0) = 0x601000 brk(0x622000) = 0x622000 write(4294967295, "hi\0", 3) = -1 EBADF (Bad file descriptor) 

Am I surprised that such a low-level operation is not related to syscall?

+6
source share
3 answers

Not every malloc call causes a system call. On my Linux desktop, malloc allocates a space of 128 KB blocks, and then allocates the space. Therefore, I will see syscall every 100-200 calls to malloc. On freebsd, malloc allocates 2 MB blocks. On your machine, the numbers are likely to be different.

If you want syscall on each malloc to allocate large amounts of memory (malloc (10 * 1024 * 1024 * 1024))

+19
source

What do you think is brk? malloc absolutely refers to syscall in this example, syscall is simply not "malloc".

+3
source

malloc () calls the system brk () function (on Linux / Unix), but it only calls it if the local heap is exhausted. That is, most malloc implementations manage the heap of memory obtained with brk (), and if it is too small or too fragmented, they request more through brk ().

+3
source

Source: https://habr.com/ru/post/890379/


All Articles