Mmap() an entire large file

后端 未结 3 1326
南方客
南方客 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:48

    You don't have enough virtual memory to handle that mapping.

    As an example, I have a machine here with 8G RAM, and ~8G swap (so 16G total virtual memory available).

    If I run your code on a VirtualBox snapshot that is ~8G, it works fine:

    $ ls -lh /media/vms/.../snap.vdi
    -rw------- 1 me users 9.2G Aug  6 16:02 /media/vms/.../snap.vdi
    $ ./a.out /media/vms/.../snap.vdi
    Size: 9820000256 
    [0]=3C [1]=3C [2]=3C [3]=20 [4]=4F [5]=72 [6]=61 [7]=63 [8]=6C [9]=65 
    

    Now, if I drop the swap, I'm left with 8G total memory. (Don't run this on an active server.) And the result is:

    $ sudo swapoff -a
    $ ./a.out /media/vms/.../snap.vdi
    Size: 9820000256 
    mmap: Cannot allocate memory
    

    So make sure you have enough virtual memory to hold that mapping (even if you only touch a few pages in that file).

提交回复
热议问题