3 ms·
None of them unmap freed memory.
by ncmncm 4y ago
None of them unmap freed memory.
- yakubin 4y agoThat depends on the specific implementation. glibc does unmap memory[1]. Not in every free() call, but still. There are also implementations which unmap in every free(), which are used to quickly detect bugs. Honestly, I wouldn't want to use an implementation which never unmaps memory. I've already been irritated by Go's reluctance to return memory to the OS in the past. I'm certainly glad it's less of a problem in C. (Although I've read some reports of glibc being a bit suboptimal here in multithreaded programs, compared to allocators like tcmalloc or jemalloc.) [1]: <https://github.com/bminor/glibc/blob/a364a3a7090b82ddd30e9209df2af56e781d51e4/malloc/malloc.c#L3355 https://github.com/bminor/glibc/blob/a364a3a7090b82ddd30e920...>
- ncmncm 4y agoIt won't do that by default. You need to set some non-portable flags to get it to munmap things. Unmapping trashes page-map caches of other threads in the process. That involves inter-core interrupts. Not pretty.
- yakubin 4y agoExample program: #include <stdlib.h> int main() { for (size_t i = 0; i < 4096*4096; i++) { void* p = malloc(i * 4096); free(p); } } Running strace on it gives me a whole spam of logs like: mmap(NULL, 139264, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46f000 munmap(0x7f0e7d46f000, 139264) = 0 mmap(NULL, 143360, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46e000 munmap(0x7f0e7d46e000, 143360) = 0 mmap(NULL, 147456, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46d000 munmap(0x7f0e7d46d000, 147456) = 0 mmap(NULL, 151552, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46c000 munmap(0x7f0e7d46c000, 151552) = 0 mmap(NULL, 155648, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46b000 munmap(0x7f0e7d46b000, 155648) = 0 mmap(NULL, 159744, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0e7d46a000 munmap(0x7f0e7d46a000, 159744) = 0 That was run under a standard Debian 11 installation.
- ncmncm 4y agoEek. I am corrected. Now I need to find the nonstandard flags to set to turn it off.