I'm studying operating systems at university and one of my tasks was find situation when malloc() doesn't cause mmap() system call.
I used strace linux utility to trace system calls, but in my situation I saw mmap() syscalls every time when malloc() was used.
Is malloc() always call mmap() or not?
Thanks
This is defined neither by C, C++ nor the POSIX standard.
Is malloc() always call mmap() or not?
Not necessarly. This depends on the malloc implementation, configuration and the size of the allocation and possibly other factors.
If using glibc:
Tunable: glibc.malloc.mmap_threshold
This tunable supersedes the MALLOC_MMAP_THRESHOLD_ environment variable and is identical in features.
When this tunable is set, all chunks larger than this value in bytes are allocated outside the normal heap, using the mmap system call. This way it is guaranteed that the memory for these chunks can be returned to the system on free. Note that requests smaller than this threshold might still be allocated via mmap.
If this tunable is not set, the default value is set to ‘131072’ bytes and the threshold is adjusted dynamically to suit the allocation patterns of the program. If the tunable is set, the dynamic adjustment is disabled and the value is set as static.
If not using glibc, then consult the documentation or source of the implementation that you use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With