{"id":5317,"date":"2021-02-23T16:01:01","date_gmt":"2021-02-24T00:01:01","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/pix\/?p=5317"},"modified":"2021-02-24T17:35:12","modified_gmt":"2021-02-25T01:35:12","slug":"memory-leak-detection-features-in-timing-captures","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/pix\/memory-leak-detection-features-in-timing-captures\/","title":{"rendered":"Finding memory leaks using Timing Captures"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/timing-captures-new\/\">PIX Timing Captures<\/a> include several analysis features aimed at making it easy to find memory leaks in your title.\u00a0 These features, coupled with the ability to take captures for extended periods of time, are a compelling combination for finding leaks, even those that<span style=\"font-size: 1rem;\">\u00a0can take a considerable amount of time to manifest themselves.<\/span><\/p>\n<p>To enable the leak tracking features, select one or both of the memory tracking options when starting a Timing Capture.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5322\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options.png\" alt=\"Image mem leaks memory options\" width=\"787\" height=\"1137\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options.png 787w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options-208x300.png 208w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options-709x1024.png 709w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_options-768x1110.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/a><\/p>\n<p>The easiest way to visualize the amount of memory in use over the duration of the capture is to use the Metrics View.\u00a0 The <strong>Memory Usage<\/strong> counters under the <strong>Derived System Metrics\/CPU Memory<\/strong> node in the <strong>Selector<\/strong> panel can be used to view the amount of memory in use due to calls to HeapAlloc and VirtualAlloc.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_counters.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5324\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_counters.png\" alt=\"Image mem leaks memory counters\" width=\"511\" height=\"781\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_counters.png 511w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_memory_counters-196x300.png 196w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><\/p>\n<p>The following picture shows a graph of the amount of memory allocated by VirtualAlloc.\u00a0 The amount of memory allocated is clearly growing over time.\u00a0 There are periods of time in which usage is flat, following by periods of time where memory usage grows and doesn&#8217;t appear to be freed by the end of the capture.\u00a0 The periods of memory growth may indicate a leak.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5327\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth.png\" alt=\"Image mem leaks va growth\" width=\"1780\" height=\"942\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth.png 1780w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth-300x159.png 300w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth-1024x542.png 1024w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth-768x406.png 768w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_growth-1536x813.png 1536w\" sizes=\"(max-width: 1780px) 100vw, 1780px\" \/><\/a><\/p>\n<p>To determine if that&#8217;s the case, select a range of time in the Metrics view that corresponds to one or more periods of memory growth.\u00a0 We&#8217;ll then use the <strong>Range Details<\/strong> view to analyze an <strong>Allocation Stack Tree<\/strong> for the selected period of time.<\/p>\n<p>To make the analysis easier, I&#8217;ve created a custom layout that adds a <strong>Range Details<\/strong> view to the predefined Metrics View layout.\u00a0 I&#8217;ve also used the <strong>Display Options<\/strong> panel in <strong>Range Details<\/strong> to set the <strong>Time Range<\/strong> dropdown to <strong>Sync with Metric View&#8217;s Selected Range<\/strong>.\u00a0 Note that creating this custom layout isn&#8217;t required for analyzing memory leaks, I&#8217;ve just set it up this was as a convenience.\u00a0 The <strong>Range Details<\/strong> view in the standard <strong>Timeline<\/strong> layout can be used as well.<\/p>\n<p>Here&#8217;s my custom layout, with an area of growth selected in the graph and the <strong>Range Details<\/strong> viewing showing the <strong>Allocation Stack Tree<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5328\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout.png\" alt=\"Image mem leaks custom layout\" width=\"1820\" height=\"1188\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout.png 1820w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout-300x196.png 300w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout-1024x668.png 1024w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout-768x501.png 768w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_custom_layout-1536x1003.png 1536w\" sizes=\"(max-width: 1820px) 100vw, 1820px\" \/><\/a><\/p>\n<p>The allocations in the stack tree are grouped by allocator.\u00a0 The tree has two root nodes: one for those allocations that came from calls to VirtualAlloc and one for those that came from calls to HeapAlloc.\u00a0 Given that we know our leak is coming from virtual memory allocations,\u00a0 we&#8217;ll start expanding the tree and monitoring the values in the <strong>Unfreed<\/strong> <strong>Size<\/strong> column to find allocations that weren&#8217;t freed.<\/p>\n<p>The following <strong>Range Details<\/strong> view shows a function in my title, named ResizeBuffer, that is leaking 560MB of VirtualAlloc&#8217;d memory.\u00a0 Furthermore, I can see there are two call paths into ResizeBuffer, one callpath comes from a function named Process and another callpath comes from a function named UpdateEnemyPositions.\u00a0 The <strong>Unfreed Size<\/strong> column shows that all of the leaked memory is coming from the call to ResizeBuffer that comes from Process.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5330\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks.png\" alt=\"Image mem leaks va leaks\" width=\"1241\" height=\"397\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks.png 1241w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks-300x96.png 300w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks-1024x328.png 1024w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_va_leaks-768x246.png 768w\" sizes=\"(max-width: 1241px) 100vw, 1241px\" \/><\/a><\/p>\n<p>Drilling into the stack and using the <strong>Unfreed Size<\/strong> column is one way to find leaks, but given that the memory leak scenario is such a common workflow, PIX includes a shortcut to make this even easier. To find the call tree(s) in your title that are causing leaks, simply use the <strong>!Freed<\/strong> button at the top of the <strong>Range Details<\/strong> view.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-5331\" src=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button.png\" alt=\"Image mem leaks not freed button\" width=\"1237\" height=\"397\" srcset=\"https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button.png 1237w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button-300x96.png 300w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button-1024x329.png 1024w, https:\/\/devblogs.microsoft.com\/pix\/wp-content\/uploads\/sites\/41\/2021\/02\/mem_leaks_not_freed_button-768x246.png 768w\" sizes=\"(max-width: 1237px) 100vw, 1237px\" \/><\/a><\/p>\n<p>Easy!<\/p>\n<p>Steven Pratschner<\/p>\n<p>PIX Program Manager<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PIX Timing Captures include several analysis features aimed at making it easy to find memory leaks in your title.\u00a0 These features, coupled with the ability to take captures for extended periods of time, are a compelling combination for finding leaks, even those that\u00a0can take a considerable amount of time to manifest themselves. To enable the [&hellip;]<\/p>\n","protected":false},"author":1915,"featured_media":4769,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pix"],"acf":[],"blog_post_summary":"<p>PIX Timing Captures include several analysis features aimed at making it easy to find memory leaks in your title.\u00a0 These features, coupled with the ability to take captures for extended periods of time, are a compelling combination for finding leaks, even those that\u00a0can take a considerable amount of time to manifest themselves. To enable the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts\/5317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/users\/1915"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/comments?post=5317"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts\/5317\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/media\/4769"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/media?parent=5317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/categories?post=5317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/tags?post=5317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}