February 25th, 2016

How do I preload a chunk of memory into a memory-mapped file?

A customer had a moderate-sized memory-mapped file (a few dozen megabytes), and they wanted to preload the entire file into memory because their program accesses the contents of the file non-sequentially, which means the normal prefetcher cannot anticipate which page to preload. They are forcing the pages to be preloaded by manually walking through the file and touching every page. They wanted to know if there was a better way of accomplishing this.

Windows 8 introduces the Prefetch­Virtual­Memory function. It allows you to request that specific sections of your application’s virtual address space be preloaded from disk. By submitting a prefetch request, the memory manager can issue block I/O reads in order to satisfy the request, which is more efficient than reading each page one at a time as you touch them.

Note that prefetching virtual memory is purely advisory. The memory manager is free to ignore your request, or to honor only parts of it.

The customer wrote back that Prefetch­Virtual­Memory was exactly what they needed.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback