I am running some large array processing code (on a Pentium running Linux). The sizes of the arrays are large enough for the processes to swap. So far it is working, probably because i try to keep my read and writes contiguous. However, I will soon need to handle larger arrays. In this scenario, would switching over to anonymous mmapped blocks help ?
If so would you please explain why.
In my shallow understanding, mmap implements a memory mapped file mounted from a tmpfs partition which under memory pressure would fall back to the swapping mechanism. What I would like to understand is how does mmap do it better than the standard malloc (for the sake or argument I am assuming that it is indeed better, I do not know if it is so).
Note: Please do not suggest getting a 64 bit and more RAM. That, unfortunately, is not an option.
The memory that backs your
malloc()allocations is handled by the kernel in much the same way as the memory that backs private anonymous mappings created withmmap(). In fact, for large allocationsmalloc()will create an anonymous mapping withmmap()to back it anyway, so you are unlikely to see much difference by explicitly usingmmap()yourself.At the end of the day, if your working set exceeds the physical memory size then you will need to use swap, and whether you use anonymous mappings created with
mmap()ormalloc()is not going to change that. The best thing you can do is to try and refactor your algorithm so that it has good locality of reference, which will reduce the extent to which swap hurts you.You can also try to give the kernel some hints about your usage of the memory with the
madvise()system call.