tags:

views:

2166

answers:

4

Hi,

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

+5  A: 

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.
antonm
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).
tristopia
+2  A: 

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.

Jeffrey Aylesworth
A: 

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

joemoe
A: 

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..

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.
tristopia