In the process of running a Linux system, in order to make the computer or server run at an optimal level, it is often necessary to monitor memory statistics.
So today we’ll look at ways to access all relevant information and help administrators monitor memory statistics.
You can view or obtain the memory usage in Linux through commands or through the desktop GUI program.
The following process, all commands and results are based on the Linux distribution Ubuntu 18.04.
GUI View
If the environment you are using is a Linux version with a graphical interface, it must support the use of a graphical interface for server management. Then, visualization of memory usage will make certain data sets more clear and natural.
How to access System Monitor:
- Navigate to the display application.
- Type System Monitor in the search bar and access the application.
- Select the Resources tab.
- Shows a graphical overview of real-time memory consumption, including historical information.
command acquisition
Virtual file/proc/meminfo
In Linux systems, /proc/meminfo is a virtual file that reports the amount of free and used memory. It contains real-time information about system memory usage and buffers and shared memory used by the kernel. The output read from this file may vary slightly depending on the computer architecture and operating system version in question.
Enter command
cat /proc/meminfo
command return
command free
Type free in the command terminal
The data represents used/available memory and swap memory size in KB.
total | total installed memory |
used | The memory currently being used by the running process (used= total – free – buff/cache) |
free | Unused memory (free= total – used – buff/cache) |
shared | Memory shared by multiple processes |
buffers | Memory reserved by the operating system and allocated as a buffer when the process needs it |
cached | Recently used files stored in RAM |
buff/cache | Buffers + Cache |
available | The amount of memory that can be used to launch new applications without swapping |
The free command provides less information than reading the /proc/meminfo file, but it is easier to understand.
For more flexible output results, the free command can add multiple option flags to format the output. The following table lists the most useful option flags of the free command.
option flag | result |
-b | Output in Byte |
-k | Output in KB |
-m | Output in MB |
-G | Output in GB |
-l | Detailed low and high memory statistics |
-o | Old format (no -/+ buffer/cache lines) |
-t | RAM + swap |
-s | How many seconds does it take to update? |
-C | how many times to update |
command vmstat
The vmstat command is a useful tool for reporting virtual memory statistics. Its returns provide general information about processes, memory, paging, block IO, capture, and CPU activity.
Here is an explanation of each value for your convenience when analyzing the results.
Process
r: The number of processes waiting to run.
b: The number of processes in uninterruptible sleep state.
Memory
swpd: The size of virtual memory used.
free: free memory size.
buff: The memory size used as buffer.
cache: The memory size used as cache.
Swap area
si: memory swapped in from disk (/s).
so: Memory swapped to disk (/s).
IO blocks
bi: Blocks received from block device (blocks/s).
bo: Blocks (blocks/s) sent to the block device.
System
in: Number of interrupts per second, including clock.
cs: Number of context switches per second.
CPU – The following are percentages of total CPU time.
us: The time spent running non-kernel code (user time, including nice time)
sy: The time spent running kernel code (system time)
id: Idle time, before Linux 2.5.41, this includes IO waiting time.
wa: The time spent waiting for IO, before Linux 2.5.41, included in idle.
st: Time stolen from the virtual machine, before Linux 2.6.11, unknown.
command top
The top command can be used to check the memory and CPU usage of each process, etc. Contains the following information:
正常运行时间
平均负荷
正在运行的任务数
登录用户数
CPU 数量/CPU 利用率
内存/交换区的系统进程
After the top command is entered, the data will be continuously updated so that you can track the process status in real time.
Through the top command, in addition to seeing basic memory information, you can also interoperate with processes through a limited interactive interface. You can also use command line options when entering the top command to manipulate and configure the operation.
If you need complete information about all available options of the top command, you can view it through the man top command in Linux.
commandhtop
The htop command is similar to the top command. However, the real advantage of the htop command is its user-friendly operating environment and more powerful controls.
The htop command output includes color highlighting, a full command line for the process, and vertical and horizontal scrolling capabilities.
Compare the picture above
- The top area provides summary information and contains graphical gauges and summary statistics.
- The following sections organize the detailed data by process so that you can easily perform operations on individual processes and select processes with a direct mouse click.
- Shortcuts listed at the bottom of the screen will allow you to quickly operate and configure processes without having to enter specific instructions.
The command htop is not installed by default in this distribution. If you find that the command does not exist, you can install it through the following command.
sudo apt install htop
Learning can wait, time waits for no one!
Follow me and I will help you learn more core skills in the field of programming!