MemoryMappedFile.CreateNew(...) 是否保证创建一个包含零的文件?

Is MemoryMappedFile.CreateNew(...) guaranteed to create a file with zeroes?

正如标题所说;我可以依赖 MemoryMappedFile 来创建一个全为零的文件吗?我已经通过读取一些新创建的文件进行了测试,但是这个 "testing" 似乎是徒劳的,尽管我还没有看到 non-zero 字节。

是否有任何我遗漏的相关文档,或者仅仅是假设内存映射文件在创建时为空。

不,这不能保证。这似乎是特定于实现的。

虽然在 MemoryMappedFile 中未提及,但它在 .NET 中使用的底层原生 API 的行为 (CreateFileMapping) 确实明确记录了这一点,并且它有所不同,具体取决于存储的来源。

在文件支持映射的情况下:

If the file is extended, the contents of the file between the old end of the file and the new end of the file are not guaranteed to be zero; the behavior is defined by the file system.

在匿名映射的情况下(由页面文件支持):

The initial contents of the pages in a file mapping object backed by the operating system paging file are 0 (zero).