{"id":962,"date":"2018-10-20T10:47:06","date_gmt":"2018-10-20T07:47:06","guid":{"rendered":"http:\/\/kifarunix.com\/?p=962"},"modified":"2024-03-11T20:05:08","modified_gmt":"2024-03-11T17:05:08","slug":"how-to-measure-memory-usage-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-measure-memory-usage-in-linux\/","title":{"rendered":"How to Measure Memory Usage in Linux"},"content":{"rendered":"\n<p>Linux provides a handful tools for measuring and checking memory usage. In this tutorial, we are going to learn a quite a number of these tools so that we can be able to troubleshoot any memory related issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>free<\/strong><\/h2>\n\n\n\n<p>free command is one of the most popular commands for checking free and used physical memory as well as swap space on Linux system. It also shows the buffers and caches used by the kernel. Below are basic examples of the free command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># free -h\n              total        used        free      shared  buff\/cache   available\nMem:            15G        6.1G        3.3G        574M        6.1G        8.5G\nSwap:          7.9G          0B        7.9G<\/pre>\n\n\n\n<p>The <strong>-h<\/strong> option shows the output fields automatically scaled to shortest three digit unit and display the units of print out. You may also use <strong>-b, -k, -m, -g<\/strong> to display the output in <strong>bytes, kilobytes, megabytes, gigabytes<\/strong> respectively.<\/p>\n\n\n\n<p>The displayed columns are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>total<\/strong> &#8211; shows the total installed memory (MemTotal and SwapTotal in \/proc\/meminfo)<\/li>\n\n\n\n<li><strong>used<\/strong> &#8211; Used memory (calculated as <strong>total &#8211; free &#8211; buffers &#8211; cache<\/strong>)<\/li>\n\n\n\n<li><strong>free<\/strong> &#8211; Unused memory (MemFree and SwapFree in \/proc\/meminfo)<\/li>\n\n\n\n<li><strong>shared<\/strong> &#8211; Memory used (mostly) by tmpfs (Shmem in \/proc\/meminfo)<\/li>\n\n\n\n<li><strong>buffers<\/strong> &#8211; Memory used by kernel buffers (Buffers in \/proc\/meminfo)<\/li>\n\n\n\n<li><strong>cache<\/strong> &#8211; Memory used by the page cache and slabs (Cached and Slab in \/proc\/meminfo)<\/li>\n\n\n\n<li><strong>buff\/cache<\/strong> &#8211; Sum of buffers and cache<\/li>\n\n\n\n<li><strong>available &#8211; <\/strong>Estimation of how much memory is available for starting new applications, without swapping.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\/proc\/meminfo<\/strong><\/h2>\n\n\n\n<p><strong>\/proc\/meminfo<\/strong> reports a large amount of valuable information about the system&#8217;s RAM usage. To read this file, you can use<strong> cat<\/strong> command of paging commands like <strong>less<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong># cat \/proc\/meminfo<\/strong> \nMemTotal:       16320364 kB\nMemFree:         3385056 kB\nMemAvailable:    8779064 kB\nBuffers:          317064 kB\nCached:          5707092 kB\nSwapCached:            0 kB\nActive:          6506828 kB\nInactive:        3834708 kB\nActive(anon):    4434548 kB\nInactive(anon):   462884 kB\nActive(file):    2072280 kB\nInactive(file):  3371824 kB\nUnevictable:          48 kB\nMlocked:              48 kB\nSwapTotal:       8286204 kB\nSwapFree:        8286204 kB\nDirty:                92 kB\nWriteback:             0 kB\nAnonPages:       4317476 kB\nMapped:          3148160 kB\nShmem:            580048 kB\nSlab:             362528 kB\nSReclaimable:     288048 kB\nSUnreclaim:        74480 kB\nKernelStack:       13744 kB\nPageTables:        66156 kB\nNFS_Unstable:          0 kB\nBounce:                0 kB\nWritebackTmp:          0 kB\nCommitLimit:    16446384 kB\nCommitted_AS:   13742100 kB\nVmallocTotal:   34359738367 kB\nVmallocUsed:           0 kB\nVmallocChunk:          0 kB\nHardwareCorrupted:     0 kB\nAnonHugePages:         0 kB\nShmemHugePages:        0 kB\nShmemPmdMapped:        0 kB\nCmaTotal:              0 kB\nCmaFree:               0 kB\nHugePages_Total:       0\nHugePages_Free:        0\nHugePages_Rsvd:        0\nHugePages_Surp:        0\nHugepagesize:       2048 kB\nDirectMap4k:      306688 kB\nDirectMap2M:    12171264 kB\nDirectMap1G:     4194304 kB<\/pre>\n\n\n\n<p>Much of the information in <strong>\/proc\/meminfo<\/strong> is used by the <code class=\"command\">free<\/code>, <code class=\"command\">top<\/code>, and <code class=\"command\">ps<\/code> commands. The output of the <strong>free<\/strong> command is similar in appearance to the contents and structure of <strong>\/proc\/meminfo<\/strong>.<\/p>\n\n\n\n<p>Some of the information that you may be interested in the output of the \/proc\/meminfo include;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MemTotal<\/strong>: Total amount of usable RAM.<\/li>\n\n\n\n<li><strong>MemFree<\/strong>: The amount of physical RAM left unused by the system.<\/li>\n\n\n\n<li><strong>MemAvailable<\/strong>: An estimate of how much memory is available for starting new applications, without swapping.<\/li>\n\n\n\n<li><strong>Buffers<\/strong>: The amount of temporary storage for raw disk blocks.<\/li>\n\n\n\n<li><strong>Cached<\/strong>: The amount of physical RAM used as cache memory.<\/li>\n\n\n\n<li><strong>SwapCached<\/strong>: The amount of memory that has once been moved into swap, then back into the main memory.<\/li>\n\n\n\n<li><strong>SwapTotal<\/strong>: The total amount of swap available.<\/li>\n\n\n\n<li><strong>SwapFree<\/strong>: The total amount of swap free.<\/li>\n<\/ul>\n\n\n\n<p>Note the the units of the \/proc\/meminfo is in kibibytes (KiB; 1 KiB equals 1024 B).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>top<\/strong><\/h2>\n\n\n\n<p>The top program provides a dynamic real-time view of a running system. This also enables you to check per process memory usage. To use top command to check per process memory, just run the command and press <strong>Shift+m <\/strong>to sort processes by memory usage in descending order.<\/p>\n\n\n\n<figure class=\"wp-block-image alignnone\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/10\/top-memory-usage.gif\"><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"443\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/10\/top-memory-usage.gif\" alt=\"top-memory-usage\" class=\"wp-image-1007\" title=\"\"><\/a><figcaption class=\"wp-element-caption\">top-memory-usage<\/figcaption><\/figure>\n\n\n\n<p>You can also override the top command sort field by passing <strong>-o fieldname<\/strong> option. You can prepend a `+&#8217; or `-&#8216; to the field name to also override the sort direction. A leading `+&#8217; will force sorting high to low, whereas a `-&#8216; will ensure a low to high ordering. See example below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">top -o +%MEM<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>htop<\/strong><\/h4>\n\n\n\n<p>Just like top command, htop is an interactive process viewer. It shows per process memory usage along with various other details. To sort the htop out put with memory usage field, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">htop -s PERCENT_MEM<\/pre>\n\n\n\n<figure class=\"wp-block-image alignnone\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/10\/htop-memory-usage.png\"><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"437\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/10\/htop-memory-usage.png\" alt=\"htop-memory-usage\" class=\"wp-image-1008\" title=\"\"><\/a><figcaption class=\"wp-element-caption\">htop-memory-usage<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>vmstat<\/strong><\/h2>\n\n\n\n<p>when <strong>vmstat <\/strong>command is run with <strong>-s<\/strong> or <strong>&#8211;stats<\/strong> option displays a table of various event counters and memory statistics. You can display the units in megabytes by passing the <strong>-S M<\/strong> option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>vmstat -sS M<\/strong>\n    15937 M total memory\n     8410 M used memory\n     7848 M active memory\n     4217 M inactive memory\n      151 M free memory\n      348 M buffer memory\n     7026 M swap cache\n     8091 M total swap\n        0 M used swap\n     8091 M free swap\n  3197280 non-nice user cpu ticks\n    14302 nice user cpu ticks\n  8691738 system cpu ticks\n 18168361 idle cpu ticks\n   202962 IO-wait cpu ticks\n        0 IRQ cpu ticks\n    68784 softirq cpu ticks\n        0 stolen cpu ticks\n 59018119 pages paged in\n 35515154 pages paged out\n        0 pages swapped in\n        0 pages swapped out\n241106787 interrupts\n581490579 CPU context switches\n1539852430 boot time\n  5186282 forks<\/pre>\n\n\n\n<p>Well, so far so good, we have described and learnt a few simple commands that can enable us to track memory usage on a Linux machine. I am sure this article has been of help. Thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux provides a handful tools for measuring and checking memory usage. In this tutorial, we are going to learn a quite a number of these<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[128,152,172],"tags":[185,58,52,51,184],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-lpic-2","category-capacity-planning","category-lpic-2-exam-201-405-topics-and-objectives","tag-proc-meminfo","tag-free","tag-htop","tag-top","tag-vmstat","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/962"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=962"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":21035,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/962\/revisions\/21035"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}