The Malloc (main memory allocation point)is the main memory allocation interface. It is the best to allocate memory and returns a pointer to the freshly available memory. mmap and malloc are memory-related terms in a UNIX system. mmap refers to a memory mapping system that has its own unique I/O. It reads a file from disk, loads it into memory, and presents a memory-mapped file.
The memory section can either be read-only or writable. mmap is not recommended for allocation of memory. mmap can also be used to speed up responses given by applications, but this comes at the expenses of losing some byte to pages so that applications can run smoothly.
F. Daniel, Content Optimization Executive, Diploma in Journalism, California
Answered Jul 16, 2019
"Malloc" stands for main memory allocation point, while "mmap" denotes a memory mapped system that has its own unique I/O. Though these two seem to be performing the same functions, still there are some differences between them, basically on functionality. Malloc is the main memory allocation interface, which is the largest in the C library. Malloc generally functions in many of the memory management processes, and mmap uses the context switch which converts into kernel land.
As against the use of mmap, malloc is best used for the allocation of memory in the running of any application on the system. Though not advisable because it sacrifices some bytes to pages, but mmap can be used to increase the speed of response received by the application. It is costly to use mmap to allocate memory basically due to data allocation and deallocation in the chunks. The memory used by malloc does not return to the OS, only if there is a segment break in data; but mmap returns the memory it has used. This gives mmap more advantage than malloc.