{"id":1763,"date":"2014-02-14T07:00:00","date_gmt":"2014-02-14T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/02\/14\/debugging-diagnosing-why-malloc-is-failing\/"},"modified":"2014-02-14T07:00:00","modified_gmt":"2014-02-14T07:00:00","slug":"debugging-diagnosing-why-malloc-is-failing","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140214-00\/?p=1763","title":{"rendered":"Debugging: Diagnosing why malloc is failing"},"content":{"rendered":"<p>A customer had some code which was experiencing memory allocation failures when calling <code>malloc<\/code> (which maps to <code>Heap&shy;Alloc<\/code>). The function returns <code>nullptr<\/code>, and <code>Get&shy;Last&shy;Error()<\/code> reports <code>ERROR_NOT_ENOUGH_MEMORY<\/code>. However, there was still plenty of memory free:<\/p>\n<ul>\n<li>Task Manager reported working set at around 400MB,     with a peak of 550MB. <\/li>\n<li>Using the <code>_heap&shy;walk<\/code> function     to compute the total memory used resulted in about     380MB being reported. <\/li>\n<li>The <code>_heap&shy;chk<\/code> function reported     no errors. <\/li>\n<li>The virtual memory size for the process was     a little bit more than the working set size. <\/li>\n<\/ul>\n<p> The customer was continuing their investigation but was looking for some pointers since the bug took a day to emerge. Could it be heap fragmentation? (The program is uses the regular C runtime heap and does not enable the low-fragmentation heap.)\n One of the suggestions was to run the <a href=\"http:\/\/technet.microsoft.com\/en-us\/sysinternals\/dd535533.aspx\"> VMMap<\/a> utility to see if the problem was exhaustion of virtual address space.\n And lo and behold, that was indeed the cause. The code had a bug where it was leaking threads. Since the default stack reservation for a thread is 1MB (although typically only a tiny fraction of that ends up being committed and even less being charged against working set), a slow accumulation of threads corresponds to a slow erosion of the virtual address space until you eventually run out.<\/p>\n<p> Once again, <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/06\/28\/10429807.aspx\"> it&#8217;s the address space, stupid<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer had some code which was experiencing memory allocation failures when calling malloc (which maps to Heap&shy;Alloc). The function returns nullptr, and Get&shy;Last&shy;Error() reports ERROR_NOT_ENOUGH_MEMORY. However, there was still plenty of memory free: Task Manager reported working set at around 400MB, with a peak of 550MB. Using the _heap&shy;walk function to compute the total [&hellip;]<\/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-1763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer had some code which was experiencing memory allocation failures when calling malloc (which maps to Heap&shy;Alloc). The function returns nullptr, and Get&shy;Last&shy;Error() reports ERROR_NOT_ENOUGH_MEMORY. However, there was still plenty of memory free: Task Manager reported working set at around 400MB, with a peak of 550MB. Using the _heap&shy;walk function to compute the total [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1763","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=1763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1763\/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=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}