正如标题所说;我可以依赖 MemoryMappedFile 来创建一个全为零的文件吗?我已经通过读取一些新创建的文件进行了测试,但这种“测试”似乎是徒劳的,尽管我还没有看到非零字节。
是否有任何我遗漏的相关文档,或者只是假设内存映射文件在创建时为空。
最佳答案
不,这不能保证。这似乎是特定于实现的。
虽然在 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).
关于c# - MemoryMappedFile.CreateNew(...) 是否保证创建一个带零的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714650/