By mmap RAM is not granted.
Address space is granted.
When the address space is accessed a page fault becomes.
During the page fault in page size, 4096 bytes typically,
RAM is provided.
RAM content also is provided.
If by a file the address space is backed
then file content will appear.
If by MAP_ANONYMOUS the address space is backed
then zero initialized RAM appears.
By the above two boons are described.
First, exactly as desired RAM can be initialized.
Second, until required RAM is not provided.
For a less than 2 megabyte address request
by malloc the program break is expanded.
While addresses close to the program break are being provided
the program break can not be contracted.
Therefore, to the kernel freed RAM might not be returned.
An analogy follows.
Can socks be removed before shoes?
By munmap invocation to the kernel RAM is immediately returned.
By mmap and munmap use swap probability is mitigated.
By malloc program break expansion swap probability is incited.
By malloc less than page size memory can be allocated.
Discontinuous memory becomes.
Kernel memory also can fragment.
Neither is perfect.
On any idle processor by the kernel RAM can be defraged.
2 megabyte sized transparent huge pages are created.
As compared with 512 page faults to provide 2M
When by a single page fault 2M can be provided
a significant performance boon becomes.
By mmap at least one notable bane exists.
For a mmap backing a pipe file descriptor can be used.
An error does not become.
However, in the memory address
the pipe provided data does not appear.
However, if MAP_ANONYMOUS is used
then from the pipe file descriptor
into the mmap provided address the data can be read.
While not as efficient the desired outcome becomes.
By a lseek failed return and errno
a pipe attached file descriptor can be identified.
By computers that can address an entire megabyte and
run a disk based operating system
then malloc use is essential.
If using C library provided getline function
then malloc and free will probably be used.
On a kernel controlled operating system
instead of mmap why use malloc?
Compared to malloc;
mmap seems complicated?
To invoke munmap
the previously requested address space amount
must also be provided.
malloc use is more portable?
malloc seems more convenient?
Yet if performance is desired then mmap is used.
Last, but not least if MAP_SHARED
then with progeny processes data can be shared.
Avoiding pthreads is paramount.
Sometimes clone also can be avoided.
Although subjective,
variable allocation methods listed
in the most to least preferred follows:
register/stack; mmap; global; malloc.
By each different boons and banes become.
By a sufficiently complicated program;
three or possibly all four methods are used.
write
ing the whole file actually sends all those bytes to disk. mmap just means if you modify themmap
ed data, then the OS will write the changes. So if you end up not modifying the whole file, you might only ever actually write a fraction of it. – Lagrangemmap
allows for other uses also, like building a shared memory area with which your process can communicate with forked precesses. – Arlon