{"id":111061,"date":"2025-04-09T07:00:00","date_gmt":"2025-04-09T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111061"},"modified":"2025-04-09T16:51:25","modified_gmt":"2025-04-09T23:51:25","slug":"20250409-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250409-00\/?p=111061","title":{"rendered":"Why can&#8217;t I use <CODE>SEC_<WBR>LARGE_<WBR>PAGES<\/CODE> with a file-based file mapping?"},"content":{"rendered":"<p>A customer wanted to create a memory-mapped file with large pages. They took <a title=\"Creating a File Mapping Using Large Pages\" href=\"https:\/\/learn.microsoft.com\/windows\/win32\/memory\/creating-a-file-mapping-using-large-pages\"> the sample code<\/a> and adapted it by changing<\/p>\n<pre>    hMapFile = CreateFileMapping(\r\n         INVALID_HANDLE_VALUE,    \/\/ use paging file\r\n         NULL,                    \/\/ default security\r\n         PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES,\r\n         0,                       \/\/ max. object size\r\n         size,                    \/\/ buffer size\r\n         szName);                 \/\/ name of mapping object\r\n<\/pre>\n<p>to<\/p>\n<pre>    hMapFile = CreateFileMapping(\r\n         <span style=\"border: solid 1px currentcolor;\">hFile,                   \/\/ use a specific file<\/span>\r\n         NULL,                    \/\/ default security\r\n         PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES,\r\n         0,                       \/\/ max. object size\r\n         size,                    \/\/ buffer size\r\n         szName);                 \/\/ name of mapping object\r\n<\/pre>\n<p>However, this failed with <code>ERROR_<wbr \/>INVALID_<wbr \/>PARAMETER<\/code>.<\/p>\n<p>The reason for the failure is documented: The <code>SEC_<wbr \/>LARGE_<wbr \/>PAGES<\/code> flag cannot be used with file-based file mappings. It can be used only with pagefile-based file mappings.<\/p>\n<p>But why?<\/p>\n<p>Recall that on Windows, <a title=\"Some remarks on VirtualAlloc and MEM_LARGE_PAGES\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110128-00\/?p=11643\"> large pages are not pageable<\/a>. A non-pageable mapping backed by the pagefile is basically memory that can never be paged out. The &#8220;backed by the pagefile&#8221; is just an accounting artifact; the memory never actually goes to the pagefile since it never pages out.<\/p>\n<p>Although you can ensure that large pages that are file-backed never get paged out, you still have another problem: Mappings that are backed by a file still need to be flushed when the mapping closes. This means that you have a large page&#8217;s worth of I\/O (or more likely, multiples of them) all pending when the handle closes, and that&#8217;s not something the memory manager has bothered implementing.<\/p>\n<p>Recall that the original audience for large pages is high performance computing scenarios where the entire system is dedicated to running a single program like SQL Server, and those programs don&#8217;t want paging to happen in the first place. Either the entire workload fits into memory, or you need more memory. (<a title=\"An amusing story about a practical use of the null garbage collector\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180228-00\/?p=98125\">Related<\/a>.) There&#8217;s no point implementing a feature (large pages in memory-mapped files) that nobody has asked for.<\/p>\n<p>But maybe this customer wants to ask for it. If they could explain why they needed large pages for file-based mappings, that could help tip the scales toward convincing the memory manager team to implement the feature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No paging, no crying.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-111061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>No paging, no crying.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=111061"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111061\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=111061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}