Search code examples
clinuxrammmap

madvise: not understood


CONTEXT:

I run on an old laptop. I only just have 128Mo ram free on 512Mo total. No money to buy more ram.

I use mmap to help me circumvent this issue and it works quite well. C code. Debian 64 bits.

PROBLEM:

Besides all my efforts, I am running out of memory pretty quick right know and I would like to know if I could release the mmaped regions I read to free my ram.

I read that madvise could help, especially the option MADV_SEQUENTIAL.

But I don't quite understand the whole picture.

THE NEED:

To be able to free mmaped allocated memory after the region is read so that it doesn't fill my whole ram with large files. I will not read it soon so it is garbage to me. It is pointless to keep it in ram.

Update: I am not done with the file so don't want to call munmap. I have other stuffs to do with it but in another regions of it. Random reads.


Solution

  • For random read/write access to a mmap()ed file, MADV_SEQUENTIAL is probably not very useful (and may in fact cause undesired behavior). MADV_RANDOM or MADV_DONTNEED would be better options in this case. However, be aware that the kernel is free to ignore any madvise() - although in my understanding, Linux currently does not, as it tends to treat madvise() more as a command than an advisory...

    Another option would be to mmap() only selected sections of the file as needed, and munmap() them as you're done with them, perhaps maintaining a pool of some small number of currently active mappings (i.e. mapping more than one region at once if needed, but still keeping it limited).