Commands for checking memory usage in Linux

Source: Internet
Author: User
Tags egrep

The Linux operating system contains a number of tools, all of which can help you manage your system. From simple file and directory tools to very complex security commands, there's not much that you can't do on Linux. Also, while normal desktop users may not need to be familiar with these tools at the command line, they are mandatory for Linux administrators. Why? First, you need to use a Linux server with no GUI at some point. Second, command-line tools often provide more functionality and flexibility than their alternatives.

Determining memory usage is a skill that you may need for a specific application and system memory. When this happens, it's convenient to know that you have a variety of tools to help you troubleshoot. Or maybe you need to gather information about the Linux swap partition or about the RAM you installed? There are also some commands. Let's take a closer look at the various Linux command-line tools to help you check system memory usage. These tools are not difficult to use, and in this article I'll show you five ways to solve the problem.

I will be demonstrating on the UbuntuServer18.04 platform. However, you should find all of these commands in the distribution of your choice. Better yet, you don't need to install one thing (because most of these tools are included).

I want to start with the most obvious tools. The command provides a dynamic, real-time diagram of a running system. Included in the system summary is the ability to check memory usage per process. This is important because you can easily iterate through multiple iterations of the same command, consuming a different amount of memory. Although you won't find it on the server, it says you've opened Chrome and noticed that your system is slowing down. Issue a top-level command to see that Chrome has multiple processes running (one per tab-figure 1).

Figure 1: Multiple instances of chrome appearing in the top command.

Chrome is not the only application that shows multiple processes. Do you see the Firefox entry in Figure 1? This is the main process of Firefox, and the webcontent process is an open label. At the top of the output, you will see system statistics. On my machine, I have a total of 16GB available RAM, of which just over 10GB. You can then comb the entire list and see the percentage of memory each process is using.

One of the things on top is the process ID (PID) number that is found to have lost control service and is very good. With these PID, you can set up troubleshooting (or kill) the offending task.

If you want top to be friendlier, issue the command Top-o%mem, which causes top to sort all the processes by the memory used (Figure 2).

Figure 2: The memory sequencing process used at the top.

The top command also gives you a real-time update of how much of your swap space is being used.

Sometimes, however, the top may meet your needs. You may only need to look at the number of available and used memory on your system. For this, there is a free order. Free command display:

Total available and used physical memory

Total amount of swap memory in the system

Buffers and caches used by the kernel

In the terminal window, publish the command for free. The output of this command is not real-time. Instead, you'll get an instant snapshot of the free and used memory at that time (Figure 3).

Figure 3:free the output of the command is straightforward.

Of course, you can make it easier for users by adding the-M option for free, as follows: Free-m. This will report memory usage in megabytes (Figure 4).

Figure 4: Output the free command in an easier-to-read format.

Of course, if your system is even remotely modern, you might want to use the-G option (gigabytes), just like free-g.

If you need total memory, you can add the T option like this: FREE-MT. This will simply sum the amount of memory in the column (Figure 5).

Figure 5: Provides you with a free total memory column.

Another very handy tool is the vmstat. This special command is a trick pony that reports virtual memory statistics. The Vmstat command will be reported in the statistics:

Process

Memory

Pagination

Block IO

Trap

Disk

Cpu

The best way to emit vmstat is to use the-s switch, such as vmstat-s. This will report your statistics in a single column (easier to read than the default report). The Vmstat command will give you more information than you need (Figure 6), but more is always better (in this case).

Figure 6: Use the Vmstat command to check memory usage.

What if you want to learn more about the installed system RAM? To do this, you can use the Dmidecode command. This particular tool is the DMI table decoder, which dumps the system's DMI table content into a readable format. If you are unsure of the contents of the DMI table, you can use it to describe the composition of the system (and the possible evolution of the system).

To run the Dmidecode command, you do need sudo permissions. So give the order sudodmidecode-t17. The output of the command (Figure 7) can be long because it displays information about all memory type devices. Therefore, if you are unable to scroll, you may need to send the output of the command to a file as follows: Sudodmidecode-t17>dmi_infoi, or enter the less command, such as Sudodmidecode.

Figure 7:dmidecode the output of the command.

You might ask yourself, "Where do these commands get this information?" ”。 In some cases, they get it from the/proc/meminfo file. Guess what? You can read the file directly using the Less/proc/meminfo command. By using the less command, you can scroll up and down through the verbose output to find exactly what you need (Figure 8).

Figure 8:less/proc/meminfo the output of the command.

One thing you should know/proc/meminfo: 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 need to check the following values:

Memtotal

Memfree

Memavailable

Buffer

Cache

Swapcached

Swaptotal

Swapfree

If you want to use/proc/meminfo, you can use it like the egrep command: Egrep--color ' mem| cache| Swap '/proc/meminfo. This will produce an easy-to-read containing Mem,cache and swap ... List of all entries (Figure 9).

? Figure 9: Make the/proc/meminfo easier to read.

One of the first things you should do to keep learning is to read the manual page for each command. (Hacker weekly)?

Commands for checking memory usage in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.