Mmap() an entire large file

后端 未结 3 1325
南方客
南方客 2020-11-29 18:13

I am trying to \"mmap\" a binary file (~ 8Gb) using the following code (test.c).

#include 
#include 
#include          


        
3条回答
  •  离开以前
    2020-11-29 18:47

    Linux (and apparently a few other UNIX systems) have the MAP_NORESERVE flag for mmap(2), which can be used to explicitly enable swap space overcommitting. This can be useful when you wish to map a file larger than the amount of free memory available on your system.

    This is particularly handy when used with MAP_PRIVATE and only intend to write to a small portion of the memory mapped range, since this would otherwise trigger swap space reservation of the entire file (or cause the system to return ENOMEM, if system wide overcommitting hasn't been enabled and you exceed the free memory of the system).

    The issue to watch out for is that if you do write to a large portion of this memory, the lazy swap space reservation may cause your application to consume all the free RAM and swap on the system, eventually triggering the OOM killer (Linux) or causing your app to receive a SIGSEGV.

提交回复
热议问题