
{"id":5902,"date":"2009-12-30T15:51:40","date_gmt":"2009-12-30T10:21:40","guid":{"rendered":"http:\/\/froisa.com\/?p=188"},"modified":"2009-12-30T15:51:40","modified_gmt":"2009-12-30T10:21:40","slug":"free-m-2","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2009\/12\/30\/free-m-2\/","title":{"rendered":"free &#8211; Display amount of free and used memory in the system"},"content":{"rendered":"<h4><u> A UNIX Command<\/u><\/h4>\n<pre style=\"font-size:10pt;\">\n$free\n             total       used       free     shared    buffers     cached\nMem:        507008     432908      74100          0      14700     186816\n-\/+ buffers\/cache:     231392     275616\nSwap:      1485972          0    1485972\n$free -m\n             total       used       free     shared    buffers     cached\nMem:           495        422         72          0         14        182\n-\/+ buffers\/cache:        225        269\nSwap:         1451          0       1451\n$\n\n<\/pre>\n<h4><u> UNIX Explanation <\/u><\/h4>\n<pre>\nDisplay amount of free and used memory in the system.\n<\/pre>\n<p><\/p>\n<h4><u> Related Source Code Exposition <\/u><\/h4>\n<p><code><br \/>\n#define S(X) ( ((unsigned long long)(X) &lt;&gt; shift)<\/p>\n<p>const char help_message[] =<br \/>\n\"usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]\\n\"<br \/>\n\"  -b,-k,-m,-g show output in bytes, KB, MB, or GB\\n\"<br \/>\n\"  -l show detailed low and high memory statistics\\n\"<br \/>\n\"  -o use old format (no -\/+buffers\/cache line)\\n\"<br \/>\n\"  -t display total for RAM + swap\\n\"<br \/>\n\"  -s update every [delay] seconds\\n\"<br \/>\n\"  -c update [count] times\\n\"<br \/>\n\"  -V display version information and exit\\n\"<br \/>\n;<\/p>\n<p>int main(int argc, char *argv[]){<br \/>\n    int i;<br \/>\n    int count = 0;<br \/>\n    int shift = 10;<br \/>\n    int pause_length = 0;<br \/>\n    int show_high = 0;<br \/>\n    int show_total = 0;<br \/>\n    int old_fmt = 0;<\/p>\n<p>    \/* check startup flags *\/<br \/>\n    while( (i = getopt(argc, argv, \"bkmglotc:s:V\") ) != -1 )<br \/>\n        switch (i) {<br \/>\n        case 'b': shift = 0;  break;<br \/>\n        case 'k': shift = 10; break;<br \/>\n        case 'm': shift = 20; break;<br \/>\n        case 'g': shift = 30; break;<br \/>\n        case 'l': show_high = 1; break;<br \/>\n        case 'o': old_fmt = 1; break;<br \/>\n        case 't': show_total = 1; break;<br \/>\n        case 's': pause_length = 1000000 * atof(optarg); break;<br \/>\n        case 'c': count = strtoul(optarg, NULL, 10); break;<br \/>\n\tcase 'V': display_version(); exit(0);<br \/>\n        default:<br \/>\n            fwrite(help_message,1,strlen(help_message),stderr);<br \/>\n\t    return 1;<br \/>\n    }<\/p>\n<p>    do {<br \/>\n        meminfo();<br \/>\n        printf(\"             total       used       free     shared    buffers     cached\\n\");<br \/>\n        printf(<br \/>\n            \"%-7s %10Lu %10Lu %10Lu %10Lu %10Lu %10Lu\\n\", \"Mem:\",<br \/>\n            S(kb_main_total),<br \/>\n            S(kb_main_used),<br \/>\n            S(kb_main_free),<br \/>\n            S(kb_main_shared),<br \/>\n            S(kb_main_buffers),<br \/>\n            S(kb_main_cached)<br \/>\n        );<br \/>\n        \/\/ Print low vs. high information, if the user requested it.<br \/>\n        \/\/ Note we check if low_total==0: if so, then this kernel does<br \/>\n        \/\/ not export the low and high stats.  Note we still want to<br \/>\n        \/\/ print the high info, even if it is zero.<br \/>\n        if (show_high) {<br \/>\n            printf(<br \/>\n                \"%-7s %10Lu %10Lu %10Lu\\n\", \"Low:\",<br \/>\n                S(kb_low_total),<br \/>\n                S(kb_low_total - kb_low_free),<br \/>\n                S(kb_low_free)<br \/>\n            );<br \/>\n            printf(<br \/>\n                \"%-7s %10Lu %10Lu %10Lu\\n\", \"High:\",<br \/>\n                S(kb_high_total),<br \/>\n                S(kb_high_total - kb_high_free),<br \/>\n                S(kb_high_free)<br \/>\n            );<br \/>\n        }<br \/>\n        if(!old_fmt){<br \/>\n            unsigned KLONG buffers_plus_cached = kb_main_buffers + kb_main_cached;<br \/>\n            printf(<br \/>\n                \"-\/+ buffers\/cache: %10Lu %10Lu\\n\",<br \/>\n                S(kb_main_used - buffers_plus_cached),<br \/>\n                S(kb_main_free + buffers_plus_cached)<br \/>\n            );<br \/>\n        }<br \/>\n        printf(<br \/>\n            \"%-7s %10Lu %10Lu %10Lu\\n\", \"Swap:\",<br \/>\n            S(kb_swap_total),<br \/>\n            S(kb_swap_used),<br \/>\n            S(kb_swap_free)<br \/>\n        );<br \/>\n        if(show_total){<br \/>\n            printf(<br \/>\n                \"%-7s %10Lu %10Lu %10Lu\\n\", \"Total:\",<br \/>\n                S(kb_main_total + kb_swap_total),<br \/>\n                S(kb_main_used  + kb_swap_used),<br \/>\n                S(kb_main_free  + kb_swap_free)<br \/>\n            );<br \/>\n        }<br \/>\n        if(pause_length){<br \/>\n\t    fputc('\\n', stdout);<br \/>\n\t    fflush(stdout);<br \/>\n\t    if (count != 1) usleep(pause_length);<br \/>\n\t}<br \/>\n    } while(pause_length &amp;&amp; --count);<\/p>\n<p>    return 0;<br \/>\n}<br \/>\n<\/code><br \/>\n<\/p>\n<h4><u> Source Code Highlight<\/u><\/h4>\n<p><code><br \/>\n#define S(X) ( ((unsigned long long)(X) &lt;&gt; shift)<br \/>\n<\/code><\/p>\n<pre>\nit  takes  a  number,  X,  casts  it  to  a  'long  long'\n(presumably the  longest int type  on your system  ?) and\nthen it left-shifts by  10 (meaning, the the positions of\nall bits are shifted 10 places to the left) only to shift\nit back right 'shift' number of positions.\n\nsource : nickname - psuedonymous. server - irc.freenode.net\n<\/pre>\n<p><\/p>\n<h4><u> Featured Image<\/u><\/h4>\n<p><a href=\"http:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/4b81d-free-may-13-2.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/4b81d-free-may-13-2.png\" alt=\"\" title=\"free-may.13\" width=\"406\" height=\"226\" class=\"alignnone size-full wp-image-5186\" srcset=\"https:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/4b81d-free-may-13-2.png 406w, https:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/4b81d-free-may-13-2-300x167.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><br \/>\n<\/p>\n<h4><u> Related Knowledge <\/u><\/h4>\n<pre>\nfree displays the total  amount of free and used physical\nand swap  memory in  the system, as  well as  the buffers\nused by  the kernel.  The shared memory  column should be\nignored; it is obsolete.\n\nsource : debian manual for free.\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A UNIX Command $free total used free shared buffers cached Mem: 507008 432908 74100 0 14700 186816 -\/+ buffers\/cache: 231392 275616 Swap: 1485972 0 1485972 $free -m total used free shared buffers cached Mem: 495 422 72 0 14 182 -\/+ buffers\/cache: 225 269 Swap: 1451 0 1451 $ UNIX Explanation Display amount of free &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2009\/12\/30\/free-m-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;free &#8211; Display amount of free and used memory in the system&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":-1,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[792,1108],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/5902"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=5902"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/5902\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=5902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=5902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=5902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}