windows64-bitramoverlapmemory-mapped-files

Windows 64-bit: Do overlapped MMF windows mean more RAM consumption (doubling the RAM where the file views overlap)?


The question is: can Windows always guarantee that views of the same portions of file won't appear duplicated in RAM, leading to unnecessary memory consumption?

Let's take an extreme example on 64-bit Windows. Say, the granularity is 64K, as usual. I map 16 pieces of window of 1 MB each from the same file, but these windows are overlapped each other just by the granulation offset, say, first maps 0..FFFFF offset from the beginning of the physical file, the next 10000..10FFFF, 20000..11FFFF and so on, up to F0000..1FFFFF. As there are actually different views of the same physical file, if the OS (and the CPU) was enough smart the actual RAM consumption would be just around 2 MB (minus 64K), not 2 × 16 MB.

enter image description here

Would it be the case or not? If so, from the user's point of view will such a memory window be shown as continuous memory block down to assembly level?


Solution

  • Windows will not duplicate the contents in memory. When pages of a file are mapped into your address space, it knows which page in the file that is.

    You can confirm this to yourself by using VMMap or RAMMap; which show you what every page in your process is, or what ever page in RAM is.

    Bonus Reading

    Old New Thing: Are views of memory-mapped files coherent within a single process? (And how this was the wrong question.) archive