Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
5 Commands for Checking Memory Usage in Linux

#1
5 Commands for Checking Memory Usage in Linux

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/5-commands-for-checking-memory-usage-in-linux.jpg" width="796" height="387" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/5-commands-for-checking-memory-usage-in-linux.jpg" class="ff-og-image-inserted" /></div>
<p>The Linux operating system includes a plethora of tools, all of which are ready to help you administer your systems. From simple file and directory tools to very complex security commands, there’s not much you can’t do on Linux. And, although regular desktop users may not need to become familiar with these tools at the command line, they’re mandatory for Linux admins. Why? First, you will have to work with a GUI-less Linux server at some point. Second, command-line tools often offer far more power and flexibility than their GUI alternative.</p>
<p>Determining memory usage is a skill you might need should a particular app go rogue and commandeer system memory. When that happens, it’s handy to know you have a variety of tools available to help you troubleshoot. Or, maybe you need to gather information about a Linux swap partition or detailed information about your installed RAM? There are commands for that as well. Let’s dig into the various Linux command-line tools to help you check into system memory usage. These tools aren’t terribly hard to use, and in this article, I’ll show you five different ways to approach the problem.</p>
<p>I’ll be demonstrating on the <a href="https://www.ubuntu.com/download/server">Ubuntu Server 18.04 platform</a>. You should, however, find all of these commands available on your distribution of choice. Even better, you shouldn’t need to install a single thing (as most of these tools are included).</p>
<p>With that said, let’s get to work.</p>
<h3>top</h3>
<p>I want to start out with the most obvious tool. The<em> </em><em>top</em> command provides a dynamic, real-time view of a running system. Included in that system summary is the ability to check memory usage on a per-process basis. That’s very important, as you could easily have multiple iterations of the same command consuming different amounts of memory. Although you won’t find this on a headless server, say you’ve opened Chrome and noticed your system slowing down. Issue the top command to see that Chrome has numerous processes running (one per tab – Figure 1).</p>
<p>Chrome isn’t the only app to show multiple processes. You see the Firefox entry in Figure 1? That’s the primary process for Firefox, whereas the Web Content processes are the open tabs. At the top of the output, you’ll see the system statistics. On my machine (a <a href="https://system76.com/desktops/leopard">System76 Leopard Extreme</a>), I have a total of 16GB of RAM available, of which just over 10GB is in use. You can then comb through the list and see what percentage of memory each process is using.</p>
<p>One of the things <em>top</em> is very good for is discovering Process ID (PID) numbers of services that might have gotten out of hand. With those PIDs, you can then set about to troubleshoot (or kill) the offending tasks.</p>
<p>If you want to make <em>top</em> a bit more memory-friendly, issue the command <em>top -o %MEM</em>, which will cause top to sort all processes by memory used (Figure 2).</p>
<p>The <em>top</em> command also gives you a real-time update on how much of your swap space is being used.</p>
<h3>free </h3>
<p>Sometimes, however, top can be a bit much for your needs. You may only need to see the amount of free and used memory on your system. For that, there is the free command. The <em>free</em> command displays:</p>
<ul>
<li>
<p>Total amount of free and used physical memory</p>
</li>
<li>
<p>Total amount of swap memory in the system</p>
</li>
<li>
<p>Buffers and caches used by the kernel</p>
</li>
</ul>
<p>From your terminal window, issue the command <em>free</em>. The output of this command is not in real time. Instead, what you’ll get is an instant snapshot of the free and used memory in that moment (Figure 3).</p>
<p>You can, of course, make <em>free</em> a bit more user-friendly by adding the <em>-m</em> option, like so:<em> </em><em>free -m</em>. This will report the memory usage in MB (Figure 4).</p>
<p>Of course, if your system is even remotely modern, you’ll want to use the <em>-g</em> option (gigabytes), as in <em>free -g</em>. </p>
<p>If you need memory totals, you can add the<em> </em><em>t </em>option like so: <em>free -mt</em>. This will simply total the amount of memory in columns (Figure 5).</p>
<h3>vmstat </h3>
<p>Another very handy tool to have at your disposal is <em>vmstat</em>. This particular command is a one-trick pony that reports virtual memory statistics. The <em>vmstat</em> command will report stats on:</p>
<ul>
<li>
<p>Processes</p>
</li>
<li>
<p>Memory</p>
</li>
<li>
<p>Paging</p>
</li>
<li>
<p>Block IO</p>
</li>
<li>
<p>Traps</p>
</li>
<li>
<p>Disks</p>
</li>
<li>
<p>CPU</p>
</li>
</ul>
<p>The best way to issue <em>vmstat</em> is by using the <em>-s</em> switch, like <em>vmstat -s</em>. This will report your stats in a single column (which is so much easier to read than the default report). The <em>vmstat</em><em> </em>command will give you more information than you need (Figure 6), but more is always better (in such cases).</p>
<h3>dmidecode</h3>
<p>What if you want to find out detailed information about your installed system RAM? For that, you could use the dmidecode command. This particular tool is the DMI table decoder, which dumps a system’s DMI table contents into a human-readable format. If you’re unsure as to what the DMI table is, it’s a means to describe what a system is made of (as well as possible evolutions for a system). </p>
<p>To run the dmidecode command, you do need <em>sudo</em> privileges. So issue the command <em>sudo dmidecode -t 17</em>. The output of the command (Figure 7) can be lengthy, as it displays information for all memory-type devices. So if you don’t have the ability to scroll, you might want to send the output of that command to a file, like so: sudo dmidecode –<em>t 17 &gt; dmi_infoI</em>, or pipe it to the <em>less</em> command, as in <em>sudo dmidecode | less</em>.</p>
<h3>/proc/meminfo</h3>
<p>You might be asking yourself, “Where do these commands get this information from?”. In some cases, they get it from the /proc/meminfo file. Guess what? You can read that file directly with the command<em> </em><em>less /proc/meminfo</em>. By using the <em>less</em><em> </em>command, you can scroll up and down through that lengthy output to find exactly what you need (Figure 8).</p>
<p>One thing you should know about <em>/proc/meminfo</em>: This is not a real file. Instead /pro/meminfo is a virtual file that contains real-time, dynamic information about the system. In particular, you’ll want to check the values for:</p>
<ul>
<li>
<p>MemTotal</p>
</li>
<li>
<p>MemFree</p>
</li>
<li>
<p>MemAvailable</p>
</li>
<li>
<p>Buffers</p>
</li>
<li>
<p>Cached</p>
</li>
<li>
<p>SwapCached</p>
</li>
<li>
<p>SwapTotal</p>
</li>
<li>
<p>SwapFree</p>
</li>
</ul>
<p>If you want to get fancy with /proc/meminfo you can use it in conjunction with the egrep command like so: egrep –color ‘Mem|Cache|Swap’ /proc/meminfo. This will produce an easy to read listing of all entries that contain Mem, Cache, and Swap … with a splash of color (Figure 9).</p>
<h3>Keep learning</h3>
<p>One of the first things you should do is read the manual pages for each of these commands (so <em>man top, man free, man vmstat, man dmidecode</em>). Starting with the man pages for commands is always a great way to learn so much more about how a tool works on Linux.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016