Wednesday, 6 September 2017

Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?



On Linux if I were to malloc(1024 * 1024 * 1024), what does malloc actually do?



I'm sure it assigns a virtual address to the allocation (by walking the free list and creating a new mapping if necessary), but does it actually create 1 GiB worth of swap pages? Or does it mprotect the address range and create the pages when you actually touch them like mmap does?



(I'm specifying Linux because the standard is silent on these kinds of details, but I'd be interested to know what other platforms do as well.)


Answer




Linux does deferred page allocation, aka. 'optimistic memory allocation'. The memory you get back from malloc is not backed by anything and when you touch it you may actually get an OOM condition (if there is no swap space for the page you request), in which case a process is unceremoniously terminated.



See for example http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...