malloc vs mmap in C
Asked Answered
A

7

62

I built two programs, one using malloc and other one using mmap. The execution time using mmap is much less than using malloc.

I know for example that when you're using mmap you avoid read/writes calls to the system. And the memory access are less.

But are there any other reasons for the advantages when using mmap over malloc?

Thanks a lot

Agretha answered 15/11, 2009 at 23:35 Comment(5)
Can I assume that your malloc program uses read/write or fread/fwrite to do some I/O on the file that you mmap in the other case?Onwards
Yes, actually I'm using read/write with malloc, mmap, and the using normal R/W calls. using R/W calls is faster than using malloc, I guess is because accessing the disk is faster than memory.Agretha
It's not that accessing the disk is faster than memory. Almost always, memory is much faster than disk, and malloc is not what's costing time. The mmap code is faster because for your program, mmap has resulted in either less disk access, or more efficient disk access, than whatever reads and writes you compared against. For instance, write ing the whole file actually sends all those bytes to disk. mmap just means if you modify the mmap 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.Lagrange
"I guess is because accessing the disk is faster than memory." That is very false. Because you give no code, I'm not sure what exactly you're talking about with malloc being slower than mmap. Both functions map address space, they do not necessarily allocate any physical memory. Nor do they read files. Both will allocate physical memory as you use each page, but mmap will do so by reading it from disk, which will certainly be slower (hard page faults vs soft page faults.) Read/Write syscalls may or may not be faster than the hard page faults, depending on numerous factors.Gans
mmap allows for other uses also, like building a shared memory area with which your process can communicate with forked precesses.Arlon
K
26

Look folks, contrary to common believe, mmap is indeed a memory allocation function similar to malloc..

the mmaped file is one use of it.. you can use it as memory allocation function passing -1 as file descriptor..

so.. the common use is to use malloc for tiny objects and mmap for large ones..

this is a good strategy..

i use alloca() to for function scope only variables..

Kayak answered 21/10, 2010 at 22:31 Comment(1)
Normally the allocator will use mmap itself depending on the size of the area asked with malloc. On Solaris when asking for more than 128K you will get a MAP_ANON memory mapped block. On OS/X the limit is 64K if I remember correctly, other systems and allocation libraries will have other values.Arlon
S
17

I assume that you are referring to using mmap and malloc for reading data from files. In that case you pretty much got the main point:

  • using fread/fwrite you have to make many calls to the OS.
  • using mmap you appear to get access to the entire file in one operation. This is not entirely true because the OS probably maps the file one memory page at a time, but it is still a lot faster.
Sonnnie answered 15/11, 2009 at 23:44 Comment(1)
To add to that, fread is buffered this means that if it is preceded by a fseek it will always fill his buffer completely. I had a program that read a file sequentially but preceding a fseek before every record (of size 32) reading 8192 bytes. So it ended up reading 256 times more data than necessary, plus reading was always two calls to the kernel. With mmap you have none (visible).Arlon
E
17

mmap doesn't actually load the file into memory, so it will load faster, but editing it will be slower.

Another point is that mmap doesn't use any memory, but it takes up address space. On a 64bit machine, most of the memory address space will not have memory, so you could load up huge files, say 5GB, that you would not want to malloc.

Eckenrode answered 15/11, 2009 at 23:45 Comment(1)
"Another point is that mmap doesn't use any memory" won't it be including any DMA copy too? if it doesn't use any memory then how processor can do anything about the data that file holds?Crowned
H
12

Both malloc and mmap are slow at times. It depends mostly on the usage pattern:

mmap: The kernel paging subsystem works in page sized units. This means, if you want to read a whole page from a file and want to repeatedly do that(good localization) it will be fine with mmap. Contrary, if you map that 5 Gb file and do scattered access, you'll have the kernel swap pages in and out a lot. In addition to the actual I/O the page management will take some time. If you have concerns about latency, avoid this access pattern, as the Linux page reclaim mechanism tends to be bursty and will cause noticeable lags, and the cache poisoning will slow down other processes.

malloc: It is fine when you need memory that's not in page size units. but you cannot do things like mlock() sanely. In terms of I/O the speed is very much dependent on how you do it. fread/fwrite may map pages behind the scenes, or will do buffering in userspace. Localized access will be rather fast. read/write go directly through the kernel, so small distributed accesses will still cause I/O due to cache misses, but the actual data transferred from kernel->userspace will be slightly less. I do not know if that is measurable.

Unless mlock()'ed, user pages may be swapped out/written back at any time. This takes time, too. So on systems with little memory, the variant that maps the least memory will win. With Linux kernel every system has too little memory as the unused pages are used for caching I/O, and the kernel may take noticeable time to make them available if memory use or I/O is bursty.

Hormuz answered 2/3, 2013 at 5:16 Comment(0)
M
5

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.

Moreira answered 25/3, 2016 at 3:12 Comment(2)
Nay matey, It just be needin' pirate speakin' to be makin' correctus in yer headin'.Maury
This answer actually provides some valuable insight into linux memory management.Abutilon
F
3

mmap doesn't actually read the file. It just maps it to address space. That's why it's so fast, there is no disc I/O until you actually access that region of address space.

malloc is simply a mapping of address space to memory

Fatsoluble answered 15/11, 2009 at 23:49 Comment(0)
R
0

Overall (this information is strictly related to my academic experience) you chose to use mmap to share memory between process and malloc to share memory between Threads. Example: you have to allocate a shared memory or semaphores between thread you first allocate them with malloc.

Riffraff answered 15/11, 2022 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.