Home > System Tutorial > LINUX > body text

Linux memory monitoring: understand the system running status in real time

王林
Release: 2024-02-09 20:33:20
forward
457 people have browsed it

In today's era of rapid information development, the stability of server systems has attracted more and more attention. As a key part of the server, memory often becomes a bottleneck for system performance issues. In this case, real-time monitoring of memory usage plays a vital role in ensuring system stability and performance. This article will introduce how to monitor memory usage in Linux systems and use some practical commands to understand the running status of the system in real time.

I will be demonstrating on an Ubuntu 18.04 server platform[1], but you should find all commands available in the distribution of your choice. Even better, you don’t need to install anything (as most of these tools are included with Linux systems).

With that said, let’s get to work.

top

I want to start with the most commonly used tools. The top command provides a real-time dynamic view of the running system, it checks the memory usage of each process. This is important because you can easily see multiple examples of the same command consuming different amounts of memory. While you can't see this on a server without a monitor, you've noticed that opening Chrome slows down your system. Run the top command to see that Chrome has multiple processes running (one for each tab – Figure 1).

Linux 内存监控:实时掌握系统运行状态

Figure 1: Multiple Chrome processes appear in the top command.

Chrome isn't the only app showing multiple processes. Do you see Firefox in Figure 1? That's Firefox's main process, and the Web Content process is its open tabs. At the top of the output, you'll see system statistics. On my machine (System76 Leopard Extreme[2]) I have a total of 16GB of available RAM, of which just over 10GB is in use. You can then sort through the list to see what percentage of memory each process is using.

top One of the best places to start is discovering the process ID number (PID) of a service that may have gone out of control. With these PIDs, you can troubleshoot (or kill) problematic tasks.

If you want top to display more friendly memory information, use the command top -o %MEM, which will cause top to sort all processes by the memory used by the process (Figure 2).

Linux 内存监控:实时掌握系统运行状态

Figure 2: Sorting processes by memory usage in top command

The

top command also gives you real-time updates on how much swap space is being used.

free

However, sometimes, the top command may not meet your needs. You may just want to look at your system's free and used memory. For this, Linux also has the free command. The free command displays:

  • 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, enter the free command. Its output is not real-time, instead you get an instant snapshot of the current free and used memory (Figure 3).

Linux 内存监控:实时掌握系统运行状态

Figure 3: The output of the free command is simple and clear.

Of course, you can make free appear more friendly by adding the -m option, like this: free -m. This will show memory usage in MB (Figure 4).

Linux 内存监控:实时掌握系统运行状态

Figure 4: Free command output in a more readable form.

Of course, if your system is very new, you will want to use the -g option (in GB), such as free -g.

If you need to know the total amount of memory, you can add the -t option, such as: free -mt. This will simply calculate the total amount of memory in each column (Figure 5).

Linux 内存监控:实时掌握系统运行状态

Figure 5: Provides you with free memory columns.

vmstat

Another very convenient tool is vmstat. This particular command is a little trick for reporting virtual memory statistics. The vmstat command will report about:

  • process
  • Memory
  • Pagination
  • Blocking IO
  • Interrupt
  • disk
  • CPU

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

Linux 内存监控:实时掌握系统运行状态

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

dmidecode

What if you want to find detailed information about installed system memory? To do this, you can use the dmidecode command. This particular tool is a DMI table decoder, which dumps the contents of your system's DMI tables into a human-readable format. If you're not sure what a DMI table is, it can be used to describe the makeup of a system (and its evolution).

To run the dmidecode command, you need sudo permissions. So enter the command sudo dmidecode -t 17. The output of this command (Figure 7) can be quite long because it displays information for all memory type devices. So if you can't scroll up and down, you may need to send the output of that command to a file, like: sudo dmidecode -t 17> dmi_infoI, or pass it to a less command, like sudo dmidecode | less.

Linux 内存监控:实时掌握系统运行状态

Figure 7: Output of the dmidecode command.

/proc/meminfo

You may be asking yourself: "Where do these commands get this information?" In some cases, they get it from the /proc/meminfo file. Did you guess it? You can read this file directly using the command less /proc/meminfo. By using the less command, you can scroll up and down through long output to find exactly what you need (Figure 8).

Linux 内存监控:实时掌握系统运行状态

Figure 8: Output of less /proc/meminfo command.

What you should know about /proc/meminfo: This is not a real file. In contrast /proc/meminfo is a virtual file that contains real-time dynamic information about the system. In particular, you need to check the following values:

  • All memory (MemTotal)
  • Free memory (MemFree)
  • Available memory (MemAvailable)
  • Buffers
  • File cache (Cached)
  • SwapCached
  • All swap areas (SwapTotal)
  • Free swap area (SwapFree)

If you want to use /proc/meminfo, you can connect the egrep command to use it: egrep –color’Mem | Cache | Swap’/proc/meminfo. This will produce an easy-to-read list where entries for things like Mem, Cache, and Swap will be colored (Figure 9).

Linux 内存监控:实时掌握系统运行状态

Figure 9: Making /proc/meminfo easier to read.

Whether it is a personal computer or a server system, the use of memory is essential and needs attention during use. This article introduces some practical commands for monitoring and managing memory in Linux systems, which allows us to grasp the running status of the system in real time and effectively avoid system downtime or other failures due to memory problems. Let us deeply understand and master these commands to ensure the safety and performance of the device.

The above is the detailed content of Linux memory monitoring: understand the system running status in real time. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!