New operator allocation of memory

How does the new operator allocate memory If enough memory is not available on the heap?

1 Like

There are different malloc implementations (new uses malloc underneath).

There’s a syscall called sbrk, but most modern mallocs use mmap and ask for a large piece anonymous piece of address space, and rely on kernel gradually paging it in on use.

Page faults are avoided by recycling memory, e.g. when something gets freed, it’s not returned to the OS, but rather given to whatever is the next thing that calls new.

If malloc decides to give memory back to the OS, there’s a madvise syscall that can be used.

1 Like