{"id":34583,"date":"2005-08-15T10:00:11","date_gmt":"2005-08-15T10:00:11","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/08\/15\/the-poor-mans-way-of-identifying-memory-leaks\/"},"modified":"2005-08-15T10:00:11","modified_gmt":"2005-08-15T10:00:11","slug":"the-poor-mans-way-of-identifying-memory-leaks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050815-11\/?p=34583","title":{"rendered":"The poor man&#8217;s way of identifying memory leaks"},"content":{"rendered":"<p>There is a variety of tools available for identifying resource leaks, but there&#8217;s one method that requires no tools or special compiler switches or support libraries: Just let the leak continue until the source becomes blatantly obvious.<\/p>\n<p> Nightly automated stress testing is a regular part of any project. Some teams use screen savers as the trigger, others use a custom program, still others require manual launching of the stress test, but by whatever means, after you&#8217;ve gone home for the day, your computer connects to a central server and receives a set of tests that it runs all night. <\/p>\n<p> One of the things that these overnight tests often turn up are memory leaks of one sort or another, identified by the stress team because your program&#8217;s resource usage has gone abnormally high. But how do you debug these failures? These machines aren&#8217;t running a special instrumented build with your leak detection tool, so you can&#8217;t use that. <\/p>\n<p> Instead, you use the &#8220;target-rich environment&#8221; principle. <\/p>\n<p> Suppose you&#8217;re leaking memory. After fifteen hours of continuous heavy usage, your program starts getting out-of-memory failures. You&#8217;re obviously leaking something, but what? <\/p>\n<p> Think about it: If you are leaking something, then there are going to be a lot of them. Whereas things you aren&#8217;t leaking will be few in number. Therefore, if you grab something at random, it will most likely be a leaked object! In mathematical terms, suppose your program&#8217;s normal memory usage is 15 megabytes, but for some reason you&#8217;ve used up 1693 megabytes of dynamically-allocated memory. Since only 15 megabytes of that is normal memory usage, the other 1678 megabytes must be the leaked data. If you dump a random address from the heap, you have a greater-than-99% chance of dumping a leaked object. <\/p>\n<p> So grab a dozen or so addresses at random and dump them. Odds are you&#8217;ll see the same data pattern over and over again. That&#8217;s your leak. If it&#8217;s a C++ object with virtual methods, dumping the vtable will quickly identify what type of object it is. If it&#8217;s a <a href=\"http:\/\/c2.com\/cgi\/wiki?PlainOldData\"> POD<\/a> type, you can usually identify what it is by looking for string buffers or pointers to other data. <\/p>\n<p> Your mileage may vary, but I&#8217;ve found it to be an enormously successful technique. Think of it as applied psychic powers. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a variety of tools available for identifying resource leaks, but there&#8217;s one method that requires no tools or special compiler switches or support libraries: Just let the leak continue until the source becomes blatantly obvious. Nightly automated stress testing is a regular part of any project. Some teams use screen savers as the [&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-34583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There is a variety of tools available for identifying resource leaks, but there&#8217;s one method that requires no tools or special compiler switches or support libraries: Just let the leak continue until the source becomes blatantly obvious. Nightly automated stress testing is a regular part of any project. Some teams use screen savers as the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34583","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=34583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34583\/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=34583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}