Disadvantages of using memory mapped files

六月ゝ 毕业季﹏ 提交于 2019-12-04 11:18:28

The main disadvantage of MMFs is that they consume RAM, making the file system cache less effective. Not an issue with such small files.

Another disadvantage, although surely intentional, is that you can no longer measure the cost of writing the file. That's now a job that's done by the kernel, not your program anymore. It is still being done of course, there's no such thing as a free lunch. It is concurrent with the rest of your program's execution, free threading so to speak. Keep an eye on the CPU utilization of the "System" process in Task Manager. Again, very unlikely to be a problem with such small files.

It is a micro-optimization, blown away by the cost of creating the file. Which hovers between 20 and 50 msec on a spindle disk drive. Don't forget to include that in your measurement. Writing the file data operates at memory bus speeds, upwards of 5 gigabyte/sec depending on the kind of RAM the machine has. What you cut out are just the low-level WriteFile() calls, they are now done by the kernel. You could try testing with a FileStream, use a constructor that takes the bufferSize value. Default is 4096 bytes, increase it to 32K so there's only one WriteFile() call. Chief advantage of doing it this way is that you don't have to guess the size of the MMF up front. It gets very ugly when you guessed too low.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!