Alex Rivera | Logout

Memory mapping of files vs CreateFile/ReadFile

Asked 2009-03-13T05:57:13.287
17

What are the drawbacks (if any) of using memory mapped file to read (regular sized files) over doing the same using CreateFile ReadFile combination?

Edit
Report

1 Answer

13

With ReadFile/WriteFile you have deterministic error handling semantics. When you use memory mapped files, errors are returned by throwing an exception.

In addition, if the memory mapped file has to hit the disk (or even worse, the network) your memory read may take several seconds (or even minutes) to complete. Depending on your application, this can cause unexpected stalls.

If you use ReadFile/WriteFile you can use asynchronous variants of the API to allow you to control this behavior.

You also have more deterministic performance if you use ReadFile, especially if your I/O pattern is predictable - memory mapped I/O is often random while ReadFile is almost always serial (since ReadFile reads at the current file position and advances the current file position).

answered 2009-03-15T00:29:16.330

Your Answer