Memory Mapped Files (MMF)
Being an average VB developer, I have stayed aloof from core OS concepts or Win32 API for that matter. Yesterday afternoon, I was casually browsing through the Caching App block and that is where I stumbled into MMFs. Honestly, MMFs are not totally an alien concept altogether. We all know bits about what a Paging file is - it is a file used by our OS to swap contents to and from the memory (demand paging). In essence, it is memory mapped. I was surprised to know that all executable files (exes, DLLs) are memory mapped too! Only that here, code pages are swapped to and fro from these binary images. So, while executable file is to code pages, the Page file is to Data pages.
So, mapping the contents of a file to some memory addresses in your process is all about MMFs are. So, what's the deal, how are MMFs useful? Well I could understand two uses:
- Sharing data between processes - I believe MMFs can be extended to include parts of the system page file, thereby enabling two or more processes to share data.
- Reading and writing to a very large file - If a very large file is memory mapped, then only required contents can be demand paged, thereby providing a better option than buffering all the file contents in memory. So, now I can guess how DBMS manages huge datafiles!
Creating and using MMFs is not overly simple and require the usage of some grunge Win32 API. But fortunately, the caching app block provides a good managed wrapper for the same and can be used as a reference.
This was the great article that threw some light on MMFs: Managing Memory-Mapped Files in Win32. It was published way back in 93, but I believe everything will hold in today's systems too!