3 ms·
> the implementation simply reserves the address range in your virtual address space (e.g. by using VirtualAlloc on Windows). Does anyone know what would be th
by ufo 4y ago
> the implementation simply reserves the address range in your virtual address space (e.g. by using VirtualAlloc on Windows).
Does anyone know what would be the alternatives to VirtualAlloc on other platforms?
- adrian_b 4y agoOn Linux and other UNIX-like operating systems, that would be mmap with the MAP_ANONYMOUS flag.
- deleted 4y ago[deleted]
- pixelfarmer 4y agoWith Linux+glibc, malloc() makes use of different syscalls. Beside mmap(), there is also brk() in use: https://man7.org/linux/man-pages/man2/sbrk.2.html https://man7.org/linux/man-pages/man2/sbrk.2.html The big question about all that is when this memory is actually being released back to the OS, because by default it isn't really. That also means you can trigger OOM faults despite the process having free memory even (which the OS sees differently).
- adrian_b 4y agobrk/sbrk are obsolete legacy functions. Memory allocated with mmap is released with munmap. brk/sbrk can increase the size of the data segment, thus allocating memory, but they can also decrease the size of the data segment, freeing the memory. Whether calling the function free of the standard C library results sometimes in also invoking munmap or brk/sbrk to release memory to the operating system is obviously implementation dependent. When malloc/free are bypassed and you get memory from the OS directly with mmap, to be used by a custom memory allocator, e.g. an arena allocator, then it is up to you to call munmap when the memory is no longer needed.