{"id":697,"date":"2010-05-13T09:14:26","date_gmt":"2010-05-13T14:14:26","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=697"},"modified":"2011-07-06T10:34:36","modified_gmt":"2011-07-06T15:34:36","slug":"is-memory-allocated-actually-in-use","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2010\/05\/13\/is-memory-allocated-actually-in-use\/","title":{"rendered":"Is memory allocated actually in use?"},"content":{"rendered":"<p>In an earlier <a href=\"http:\/\/appcrawler.com\/wordpress\/?p=654\" target=\"_blank\">article<\/a>, we talked about actually initializing memory rather than just allocating it.  This post will show how we can look at what is allocated in Linux, as well as when it shows as being in use and when it doesn&#8217;t.<\/p>\n<p>I found that memory will show up in pmap (really, pmap just reads \/proc\/pid\/maps and formats it) if it has been malloc\u2019d.  <\/p>\n<p>It will *not* show up in free-m or \/proc\/meminfo unless it has been initialized\/written to, i.e., calloc() or memset().<\/p>\n<p>The JRE must malloc() 1.2GB of RAM per process, but not call memset().  Once again, in this case the memory will not show up as used in free \u2013m if this is the case (as it is with us).  This is also what Oracle does with the SGA, which is why we don\u2019t see free memory reduced by 8GB immediately after we start an instance, but only over time.<\/p>\n<p>Using the C code below\u2026<\/p>\n<pre lang=\"c\" line=\"1\">\r\nint main() {\r\n  void *m = malloc(2000*1024*1024);\r\n  memset(m,0,2000*1024*1024);\r\n  sleep(30);\r\n  return 0;\r\n}\r\n<\/pre>\n<p>\u2026we start with the server showing 0MB swap used, 5500MB of file buffers\/cache, and 700MB free memory\u2026<\/p>\n<pre lang=\"text\" line=\"1\">\r\n21:05:31 oracle@emgrid01 ~ >free -m\r\n             total       used       free     shared    buffers     cached\r\nMem:          7987       7287        700          0        624       4902\r\n-\/+ buffers\/cache:       1761       6226\r\nSwap:         1004          0       1004\r\n<\/pre>\n<p>\u2026I then malloc()\/memset() the 2GB of memory\u2026<\/p>\n<pre lang=\"text\" line=\"1\">\r\n21:05:33 oracle@emgrid01 ~ >.\/allocatemem &\r\n[1] 10616\r\n<\/pre>\n<p>\u2026after which I show 2GB of memory as writable-private in my process\u2026<\/p>\n<pre lang=\"text\" line=\"1\">\r\n21:05:39 oracle@emgrid01 ~ >pmap -d 10616 | tail -1\r\n2048140K writable-private, 10580K readonly-private, and 0K shared\r\n<\/pre>\n<p>\u2026so the server now shows we have 650MB less free memory, 1400MB less file buffer\/cache used, and an increase of 80MB of swap used (why was the swap used rather than pulling from file cache??)\u2026<\/p>\n<pre lang=\"text\" line=\"1\">\r\n21:05:46 oracle@emgrid01 ~ >free -m\r\n             total       used       free     shared    buffers     cached\r\nMem:          7987       7940         47          0        302       3798\r\n-\/+ buffers\/cache:       3839       4148\r\nSwap:         1004         80        923\r\n21:05:49 oracle@emgrid01 ~ >\r\n<\/pre>\n<p>\u2026and when the process terminates, the OS dutifully cleans up behind me and we see the swap is still there, and the file buffers\/cache that were in use at the start of our test have now been returned to the free pool\u2026<\/p>\n<pre lang=\"text\" line=\"1\">\r\n[1]+  Done                    .\/allocatemem\r\n21:07:00 oracle@emgrid01 ~ >free -m\r\n             total       used       free     shared    buffers     cached\r\nMem:          7987       5935       2052          0        301       3798\r\n-\/+ buffers\/cache:       1835       6152\r\nSwap:         1004         80        923\r\n21:07:03 oracle@emgrid01 ~ >\r\n<\/pre>\n<p>If I run the test with the memset() call commented out, pmap \u2013d will still show the 2GB as writable-private in my process, but the free server memory as shown by free \u2013m will not be reduced, as it has only been allocated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an earlier article, we talked about actually initializing memory rather than just allocating it. This post will show how we can look at what is allocated in Linux, as well as when it shows as being in use and&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2010\/05\/13\/is-memory-allocated-actually-in-use\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[28,27],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/697"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":9,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":962,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/697\/revisions\/962"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}