{"id":2471,"date":"2012-11-28T06:54:57","date_gmt":"2012-11-28T14:54:57","guid":{"rendered":"http:\/\/manytricks.com\/blog\/?p=2471"},"modified":"2015-09-06T07:00:35","modified_gmt":"2015-09-06T14:00:35","slug":"witch-and-ram-usage-both-real-and-not-so-real","status":"publish","type":"post","link":"https:\/\/manytricks.com\/blog\/?p=2471","title":{"rendered":"Witch and RAM usage, both real and not so real"},"content":{"rendered":"<p><!--RGSupportContent--><a href=\"\/witch\">Witch<\/a>, our window switching application, is designed to be always-running (what good is a window switcher if it&#8217;s not active?). The program itself exists as two components: the user interface, where you modify Witch&#8217;s settings, and the background process that watches for the Witch activation keystrokes and builds the list when activated. The background process is named <tt>witchdaemon<\/tt>, and some users have emailed us with concerns about the RAM usage of this background daemon.<\/p>\n<p>The emails we receive come in two flavors:<\/p>\n<ol>\n<li>Why is Witch using so much real RAM?<\/li>\n<li>Why is Witch reserving gigabytes of virtual memory (<tt>VSIZE<\/tt> in <tt>top<\/tt>)?<\/li>\n<\/ol>\n<p>Read on for the details on both real and virtual RAM usage by Witch\u2014the explanations are somewhat detailed and technical (especially relative to virtual memory), so put on your geek glasses before proceeding.<\/p>\n<p><!--more--><\/p>\n<h3>High real RAM usage<\/h3>\n<p>I&#8217;ll address real RAM usage first, because it&#8217;s both more important and much simpler to understand. To see how much real RAM Witch is using, run Activity Monitor (in Applications > Utilities), click on the Process Name header, and find <tt>witchdaemon<\/tt> in the list of processes. Here&#8217;s how it looks on my Mac:<\/p>\n<div align=\"center\" style=\"padding-top:10px; padding-bottom:5px\"><a href=\"\/blog\/wp-content\/uploads\/2012\/11\/witchd_big.png\" title=\"The witchdaemon background process\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2012\/11\/witchd_small.png\" alt=\"The witchdaemon background process\" class=\"screenshot\"><\/a><\/div>\n<p>The column of interest is <b>Real Mem<\/b>, and as you can see, it&#8217;s only 13.4MB&mdash;and the daemon had been running for nine days when I took that screenshot. On your Mac, though, you may see a much different value, and it could be substantially higher; I&#8217;ve had reports of RAM usage exceeding 1GB. So why the differences? There are three things that affect the memory usage for <tt>witchdaemon<\/tt>:<\/p>\n<ol>\n<li>The number of windows you&#8217;re switching between. Keep more windows open, and generally speaking, <tt>witchdaemon<\/tt> will use more RAM.<\/li>\n<li>The setting of the &#8220;Show&#8221; pop-up menu in the Appearance tab of Witch&#8217;s settings. Set it to &#8216;application icons&#8217; for the least memory usage; using &#8216;mini window previews if possible&#8217; will use the most RAM.<\/li>\n<li>Using window previews, either via the &#8220;Pop up a preview of the selected window after a&#8221; pop-up menu (also on the Appearance tab of Witch&#8217;s settings), or by pressing the Space Bar with a window selected. Show lots of window previews, and RAM usage will increase.<\/li>\n<\/ol>\n<p>The reason these last two options use RAM is that Witch caches the images, so that future redraws of the panel will be quicker. For instance, here&#8217;s my same nine-days-running <tt>witchdaemon<\/tt> after I used a slew of window previews:<\/p>\n<div align=\"center\" style=\"padding-top:10px; padding-bottom:5px\"><a href=\"\/blog\/wp-content\/uploads\/2012\/11\/witchd_high_big.png\" title=\"After using window previews\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2012\/11\/witchd_high_small.png\" alt=\"After using window previews\" class=\"screenshot\"><\/a><\/div>\n<p>At present, this cache isn&#8217;t cleared aggressively by Witch, though the system will take it if it needs it. Over time, if you use preview icons and\/or window previews, you may see <tt>witchdaemon<\/tt>&#8216;s memory usage increase. (The system will do some cleanup; my RAM usage fell to 92MB within a minute or so of taking the screenshot.)<\/p>\n<p class=\"stickynote\">The <b>easiest way to reclaim this memory is to simply open the Witch settings panel<\/b>\u2014this will restart <tt>witchdaemon<\/tt> automatically, and thereby clear the cached image data.<\/p>\n<h3>High virtual memory usage<\/h3>\n<p>If you&#8217;re the type of user who likes to really see the nitty gritty on what your machine is up to, you may occasionally use <tt>top<\/tt> in Terminal. <tt>top<\/tt> is basically like Activity Monitor, but for command line geeks. If you do, and you use <a href=\"\/witch\">Witch<\/a>, it may freak you out just a bit when you see something like this:<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/2012\/11\/top_big2.png\" title=\"Output from 'top' sorted by VSIZE\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2012\/11\/top_small5.png\" class=\"screenshot\" alt=\"Output from 'top' sorted by VSIZE\"><\/a><\/p>\n<p>Why on Earth would <tt>witchdaemon<\/tt> need 11 <b>gigabytes<\/b> of <tt>VSIZE<\/tt>, whatever that might be? (In Activity Monitor, you can see these <tt>VSIZE<\/tt> values, but you must first select a process, then click Inspect; the value is on the Memory tab.)<\/p>\n<p>Regardless of what it <tt>VSIZE<\/tt> may be, though, the sheer size of the number is shocking, and probably has you thinking &#8220;Geez, Witch is a huge memory hog; that&#8217;s horrible!&#8221;<\/p>\n<p class=\"stickynote\">As it turns out that <b>this huge figure in <tt>VSIZE<\/tt> is nothing to worry about<\/b>, and it&#8217;s not exclusive to Witch. So if you&#8217;ve seen it, and wonder if you should care, the answer is you shouldn&#8217;t.<\/p>\n<p>If you&#8217;re curious as to <em>why<\/em> you shouldn&#8217;t care, then keep reading. (Things are going to get a bit geeky from here on out, so consider yourself warned.)<\/p>\n<p>You&#8217;re headed into a maze of twisty little passages, all alike&hellip;at least, that&#8217;s how I feel when I start discussing OS X and memory management. In short, it&#8217;s a very complex and powerful system that generally works very well, but can be really confusing at times. Keep in mind I&#8217;m not a programmer (Peter is our founder and programmer) or memory expert, so the following is a semi-educated amateur&#8217;s attempt at explaining Witch&#8217;s huge <tt>VSIZE<\/tt> number, and why you shouldn&#8217;t care about it.<\/p>\n<p>The <tt>VSIZE<\/tt> column represents the amount of memory &#8216;marked&#8217; for an application&#8217;s potential use. The important bit is that it&#8217;s not reserved, protected, or otherwise used; it&#8217;s simply marked, as in &#8220;Witch may need 11GB of RAM at some point.&#8221; <\/p>\n<p>If you look at the <tt>VSIZE<\/tt> figures in total, they can be shockingly large. If you look at my screenshot above, there&#8217;s a summary at the top; one of the lines summarizes the virtual memory situation:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2012\/10\/total_vsize.png\"><\/p>\n<p>That 308GB of <tt>VSIZE<\/tt> reflects the memory that has been marked as possibly required for my currently running apps&mdash;yikes! But the reality is that this amount of memory will never be used or needed. Even more important is that application authors don&#8217;t control this <tt>VSIZE<\/tt> figure; the system does it itself, based on what the author&#8217;s code needs to do. So why is it so big? The honest answer is that I don&#8217;t know why, nor seemingly does anyone out there, at least based on some internet searches.<\/p>\n<p>You can, however, see exactly what&#8217;s in that huge <tt>VSIZE<\/tt> space by using the <tt>vmmap<\/tt> tool. Enter this command in Terminal:<\/p>\n<pre>vmmap -resident `ps ax | grep [w]itchd | cut -b 1-5` | grep \"Summary for\" -A 999<\/pre>\n<p>That looks complicated, but what it does is run <tt>vmmap<\/tt> on the process ID of <tt>witchdaemon<\/tt>, and then displays only the summary portion of the output (as there are thousands of lines). The output on my machine is as follows:<\/p>\n<div style=\"margin-top:10px; margin-bottom:10px; padding: 5px; border:1px solid #CCC; width:570px; height:240px; overflow:scroll;white-space:nowrap;resize:both\">\n<pre>==== Summary for process 66301\r\nReadOnly portion of Libraries: Total=143.7M resident=80.9M(56%) swapped_out_or_unallocated=62.7M(44%)\r\nWritable regions: Total=8.4G written=7248K(0%) resident=30.0M(0%) swapped_out=0K(0%) unallocated=8.4G(100%)\r\n\r\nREGION TYPE                      VIRTUAL RESIDENT\r\n===========                      ======= ========\r\nCG backing stores                  9100K    5824K\r\nCG raster data                      228K     192K\r\nCG shared images                   1280K    1256K\r\nCoreGraphics                         16K       8K\r\nCoreServices                       6232K    6232K\r\n<span style=\"color:red\">IOKit (reserved)                  256.0M       0K        reserved VM address space (unallocated)<\/span>\r\nMALLOC                            337.2M    6520K        see MALLOC ZONE table below\r\n<span style=\"color:red\">MALLOC (reserved)                   7.8G       0K        reserved VM address space (unallocated)<\/span>\r\nMALLOC freed, no zone              72.0M     196K\r\nMALLOC guard page                   104K       0K\r\nMALLOC metadata                     740K     260K\r\nMemory tag=240                        4K       4K\r\nMemory tag=242                       12K      12K\r\nMemory tag=243                        4K       4K\r\n<span style=\"color:red\">STACK GUARD                        56.0M       0K<\/span>\r\nStack                              9752K     116K\r\nVM_ALLOCATE                        16.2M    16.1M\r\n__DATA                             12.1M    9808K\r\n__IMAGE                            1240K     344K\r\n__LINKEDIT                         36.8M    10.9M\r\n__RC_CAMERAS                        248K       0K\r\n__TEXT                            106.9M    70.0M\r\n__UNICODE                           536K     456K\r\nmapped file                        23.3M    8492K\r\nshared memory                     135.8M   111.6M\r\n===========                      ======= ========\r\nTOTAL                               8.8G   247.4M\r\nTOTAL, minus reserved VM space    825.0M   247.4M\r\n\r\n                                          VIRTUAL   RESIDENT ALLOCATION      BYTES\r\nMALLOC ZONE                                  SIZE       SIZE      COUNT  ALLOCATED  % FULL\r\n===========                               =======  =========  =========  =========  ======\r\nauto_zone_0x100263000                      256.2M      4240K      17546      1698K      0%\r\nDefaultMallocZone_0x1001d0000               72.0M      2168K       7793      1597K      2%\r\nDispatchContinuations_0x10026c000           8192K        96K          1         32      0%\r\nenviron_0x100202000                         1024K        12K          1         16      0%\r\nDefaultPurgeableMallocZone_0x1007d6000         4K         4K          0         0K      0%\r\n===========                               =======  =========  =========  =========  ======\r\nTOTAL                                      337.2M      6520K      25341      3296K      0%<\/pre>\n<\/div>\n<p>This command shows you the breakdown between what&#8217;s marked (<tt>VIRTUAL<\/tt>) and what&#8217;s being more actively used (<tt>RESIDENT<\/tt>). The lines I highlighted in red show three items that take no <tt>RESIDENT<\/tt> RAM, but have huge <tt>VIRTUAL<\/tt> values. Add them up, and of the 8.8GB in total <tt>VSIZE<\/tt>, roughly 8.1GB of it is taken up by things that aren&#8217;t using any RAM&mdash;and that aren&#8217;t controlled by Witch.<\/p>\n<p>The <tt>RESIDENT<\/tt> column shows Witch using 247MB of RAM, but that doesn&#8217;t seem to be accurate either. To me, the best summary of Witch&#8217;s actual memory usage is found in the Inspect panel in Activity Monitor; here&#8217;s what it looked like while writing this blog entry:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2012\/10\/witch_ram.png\"><\/p>\n<p>Here you can see that the <tt>witchdaemon<\/tt> process is using only 20MB of real memory, has 163MB of shared memory (used for inter-application communication, as I understand it), and has 7MB of private memory. (We are rapidly approaching the limits of my knowledge of OS X&#8217;s memory management system, so I&#8217;ll stop now!)<\/p>\n<p>In summary, Witch&#8217;s <tt>VSIZE<\/tt> figure is high. But it&#8217;s nothing we&#8217;re doing to create it ourselves, and it&#8217;s not something we can reduce. The biggest chunk of it, the 7.8GB for <tt>MALLOC (reserved)<\/tt>, isn&#8217;t anything we can remove, as it&#8217;s the OS that&#8217;s putting it there. Why is it putting it there? Who knows. But here are a couple of related articles that show it&#8217;s not a Witch problem:<\/p>\n<ul>\n<li><a href=\"http:\/\/marc-abramowitz.com\/archives\/2011\/12\/07\/dont-be-alarmed-by-super-large-vsize-on-os-x\/\">Don\u2019t be alarmed by super large VSIZE on OS X<\/a> &#8212; An example of a three-line Ruby program with a 10GB <tt>VSIZE<\/tt> value!<\/li>\n<li><a href=\"http:\/\/blog.mozilla.org\/dolske\/2007\/10\/16\/os-x-and-virtual-bloat\/\">OS X and virtual bloat<\/a> &mdash; This one goes into details on Mozilla&#8217;s large VSIZE. There&#8217;s some good info in the comments, too.<\/li>\n<\/ul>\n<p>Finally, Apple&#8217;s support article on <a href=\"http:\/\/support.apple.com\/kb\/HT1342\">Using Activity Monitor<\/a> contains some tips on how to use Activity Monitor to understand memory usage.<br \/>\n<!--\/RGSupportContent--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Witch, our window switching application, is designed to be always-running (what good is a window switcher if it&#8217;s not active?). The program itself exists as two components: the user interface, where you modify Witch&#8217;s settings, and the background process that watches for the Witch activation keystrokes and builds the list when activated. The background process [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,8],"tags":[],"coauthors":[21],"class_list":["post-2471","post","type-post","status-publish","format-standard","hentry","category-products","category-witch"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2471"}],"version-history":[{"count":5,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2471\/revisions"}],"predecessor-version":[{"id":3494,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2471\/revisions\/3494"}],"wp:attachment":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2471"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcoauthors&post=2471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}