{"id":95995,"date":"2017-04-19T07:00:00","date_gmt":"2017-04-19T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95995"},"modified":"2019-03-13T01:10:20","modified_gmt":"2019-03-13T08:10:20","slug":"20170419-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170419-00\/?p=95995","title":{"rendered":"Under what conditions could a commit of reserved memory fail?"},"content":{"rendered":"<p><!-- Tom Meschter (tomescht) win32prg 2011\/08\/26 \"Circumstances under which VirtualAlloc can fail to commit reserved memory\" Pavel calls out the commit limit --><\/p>\n<p>A customer&#8217;s program reserves a large chunk of address space (<code>Virtual&shy;Alloc<\/code> with <code>MEM_RESERVE<\/code>) and commits memory into it as necessary (<code>Virtual&shy;Alloc<\/code> with <code>MEM_COMMIT<\/code>). They received crash reports that indicated that the commit was failing, which leads to the program reporting a fatal error. Unfortunately, in between the failed <code>Virtual&shy;Alloc<\/code> and the code that generated the fatal error, the error code set by <code>Virtual&shy;Alloc<\/code> was lost. (The customer sheepishly acknowledged that this was due to &#8220;poor code&#8221;.) <\/p>\n<p>The customer wanted to know what situations could result in the failure to commit memory that had previous been successfully reserved. <\/p>\n<p>The obvious reason is that you are out of memory. The system will satisfy commit either from physical memory or from the page file.&sup1; The page file will not extend indefinitely, so you will eventually run out of memory if you keep committing. <\/p>\n<p>A less obvious reason is that the process may be running inside a job that has a commit limit. <\/p>\n<p>Once the customer knew what to look for, they were able to verify that the program running into the problem was indeed running inside a job. Furthermore, the job memory limit was set to 155MB, which is low for the type of work the program normally performs. <\/p>\n<p>Mystery solved. Of course, the next mystery is why the program is running in a job with a low commit limit, but the customer at least knew what direction to proceed next. <\/p>\n<p>&sup1; It&#8217;s not quite that way. The system does not assign a physical page or a page file page to you when you commit.&sup2; The system merely does enough bookkeeping to ensure that if you write data to the pages that you committed, then the system will have a place to hold this data and produce it upon demand. That place might be physical memory, or it might be the page file, but the system doesn&#8217;t know at commit time which it will be (and it may end up being both at different times). <\/p>\n<p>&sup2; This delay-assignment of committed pages means that reserving address space and then committing it as needed doesn&#8217;t save you any physical memory. The commit doesn&#8217;t require physical memory. The physical memory doesn&#8217;t get assigned until you try to access the memory. All you&#8217;re saving is system commit, which is just a number (although as noted in the previous footnote, the number does have a maximum value). Unless you are doing this to save large amounts of memory (dozens of megabytes or more), committing on demand is usually not worth the effort. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usually because you are out of memory, but really it&#8217;s when you&#8217;re out of commit.<\/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-95995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Usually because you are out of memory, but really it&#8217;s when you&#8217;re out of commit.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95995","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=95995"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95995\/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=95995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}