mmap fails when length is larger than 4GB

后端 未结 5 1806
攒了一身酷
攒了一身酷 2020-12-05 11:41

(The correct code is in \'Update 5\')

I tried to map a range of memory from 0x100000000 to 0x200000000 in this example C code:

#include 

        
5条回答
  •  感动是毒
    2020-12-05 12:11

    How much physical memory is there available? Linux has two distinct modes for address space allocation: Memory allocation on write (i.e. overcommit mode) or memory allocation on address space allocation. You can check by reading two files in procfs:

    cat /proc/sys/vm/overcommit_memory
    cat /proc/sys/vm/overcommit_ratio
    

    If overcommit_memory is not 0, then every address space allocation must be backed by physical memory (RAM + swap space), if overcommit_memory is 0, then memory is overcommited, i.e. the kernel will happily hand out address space, but the memory will be only allocated if data is writen to the allocated address space. And then memory is not allocated for the full reserved address space, but only for those pages that are touched. This is kinda like booking a flight ticket: Airlines usually sell more tickets than there are seats on a flight, expecting not all booked passengers will actually show up. Now you may wonder, what happens if all programs make use of the full space… Well then some nasty thing kicks in: The Linux Out Of Memory Killer will wreak havoc on your system and very likely kill those processes you need the most, due to it's arcane heuristics.

    overcommit_ratio tells the kernel

    • in overcommit mode to which ratio physical memory may be overcommited, i.e. how much more address space may be handed out, than there is physical memory.

    • in non-overcommit-mode how much spare memory to keep

    So maybe the overcommit mode just differs between the systems.

提交回复
热议问题