As the title says; Can I rely on MemoryMappedFile to always create a file full of zeroes? I've tested by reading some newly created files but this "testing" seems futile, altough I have yet to see a non-zero byte.
Is there any documentation on this that I have missed, or is it simply a given that memory mapped files are empty on creation.
No, this is not guaranteed. This appears to be implementation-specific.
While not mentioned in
MemoryMappedFile
, the behavior of the underlying native API that it uses in .NET proper (CreateFileMapping
) does explicitly document this, and it differs depending on the source of the storage.In the case of a file-backed mapping:
And in the case of an anonymous mapping (backed by page file):