{"id":67,"date":"2008-01-29T04:06:07","date_gmt":"2008-01-29T08:06:07","guid":{"rendered":"http:\/\/www.cchsu.com\/art-en\/2008\/01\/29\/67\/"},"modified":"2008-01-29T04:06:50","modified_gmt":"2008-01-29T08:06:50","slug":"the-relationship-between-memory-size-and-performance","status":"publish","type":"post","link":"http:\/\/www.cchsu.com\/art-en\/2008\/01\/29\/67\/","title":{"rendered":"The Relationship between Memory Size and Performance"},"content":{"rendered":"<p>One can easily spot an application&#8217;s memory usage in Windows using Task Manager.\u00a0 However, the numbers shown are often misintepreted and a frustration source of mine when I am bugged by some &#8220;professionals&#8221; with perfect misunderstandings.<\/p>\n<p>Memory usage has a formal name called &#8220;working set size&#8221;.\u00a0 A working set is the physical memory allocated by Windows Virtual Memory Manager (VMM) for a process.\u00a0 As a result, if one reads the book <em>Windows Internals<\/em>\u00a0and stops here, he will start complaining a program is &#8220;fatty and slow&#8221; when he sees big working set.\u00a0 Let&#8217;s read a bit further before convicting anything.\u00a0 A working set contains two parts: private working set and shared working set.\u00a0 Private working set is used only by the owning process, while shared working set is shared among processes.\u00a0 As a result, we should use private working set size as the standard of judgement, which one can use tools like <a href=\"http:\/\/www.microsoft.com\/technet\/sysinternals\/utilities\/ProcessExplorer.mspx\">Process Explorer<\/a> to inspect.<\/p>\n<p>However, the size of private working set could be deceiving.\u00a0 Windows SDK provides an API named <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms686234.aspx\">SetProcessWorkingSetSize<\/a>, which allows developers to force Windows VMM to swap all pagable pages, and eventually lower the number of private working set.\u00a0 The pitfall about the relationship between memory size and performance is that people believe smaller memory footprint implies faster execution and better performance.\u00a0 This is an over-simplified corollary.\u00a0 The actual performance killer is page fault, not memory usage.\u00a0 Programs with higher memory usage have higher probability of page faults.\u00a0 A program runs the best if all memory it demands is loaded, even if the size is huge.\u00a0 Many programs try to satisfy the &#8220;perceptual performance&#8221; based on this common pitfall and use SetProcessWorkingSetSize to beautify the numbers.\u00a0 From real performance point of view, this is actually not optimal because the use of SetProcessWorkingSetSize invites page faults.<\/p>\n<p>Working set size is a good reference but it is not directly related to the real performance of an application.\u00a0 The real killer\u00a0is page fault, especially when it is caused by memory leaks.\u00a0 Developers\u00a0care about\u00a0working set size because it&#8217;s the perfect indicator of memory leaks.\u00a0 Moreover, page fault is just one factor that affects performance, and there are many others to explore and tune.<\/p>\n<p>Windows itself actually call SetWorkingSetSize for programs, provided that the target program has a window and the window is minimized.\u00a0 I guess this is based on a pessimistic philosophy that most programs do not free unused pages right away, so Windows does it for them to boost performance.\u00a0 This interesting behavior actually introduces a very counter-intuitive\u00a0phenomenon.\u00a0 When people\u00a0found that the computer is slow, their first reaction is typically\u00a0minimize all the windows on\u00a0the desktop except the one they are working on.\u00a0 Doing so forces the SetWorkingSetSize calls and incurs more swapping, which slows down the system further.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One can easily spot an application&#8217;s memory usage in Windows using Task Manager.\u00a0 However, the numbers shown are often misintepreted and a frustration source of mine when I am bugged by some &#8220;professionals&#8221; with perfect misunderstandings. Memory usage has a formal name called &#8220;working set size&#8221;.\u00a0 A working set is the physical memory allocated by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[],"_links":{"self":[{"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/posts\/67"}],"collection":[{"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":0,"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cchsu.com\/art-en\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}