Viewing Memory in Linux
Q: I want to monitor the memory usage of a Linux system. What views or command-line tools are available in Linux?
When optimizing a Linux system, physical memory is one of the most important aspects. Naturally, Linux provides many methods to monitor the usage of this precious memory resource. The following list details various methods to view memory usage in a Linux system through visual tools or command lines.
01. <span>/proc/meminfo</span>
The simplest way to check RAM usage is through <span>/proc/meminfo</span>. This dynamically updated virtual file is actually a combination display of many other memory-related tools (such as: <span>free / ps / top</span>, etc.). <span>/proc/meminfo</span> lists all the memory usage information you want to know. Process memory usage information can also be viewed through <span>/proc//statm</span> and <span>/proc//status</span>.
$ cat /proc/meminfo
02. atop
The atop command is a monitoring command for terminal environments. It displays a comprehensive view of various system resources (CPU, memory, network, I/O, kernel) and highlights them in color under high load conditions.
$ sudo atop
03. free
The free command is a quick way to view memory usage; it provides an overview of the information collected from <span>/proc/meminfo</span>. It is the most commonly used command for monitoring system memory.
- • -m: View memory usage in MB (default is KB);
- • -b: View memory usage in bytes;
- • -s: Can monitor memory usage over a specified time period.
$ free -h
- • total: Total size of physical memory.
- • Used: Amount used.
- • Free: Amount available.
- • shared: Total memory shared by multiple processes.
- • buffers/cached: Size of disk cache.
04. GNOME System Monitor
GNOME System Monitor is a visual tool that displays the usage of CPU, memory, swap space, and network over a recent period. It also provides a way to view CPU and memory usage.
$ gnome-system-monitor
05. <span>htop</span>
<span>htop</span> command shows the real-time memory usage of each process. It provides reports on the resident memory size of all processes, total memory size of programs, shared library size, etc. The list can be scrolled horizontally and vertically.
$ htop
06. KDE System Monitor
Functions similarly to the GNOME version described in 4.
$ ksysguard
07. <span>memstat</span>
<span>memstat</span> is a command that effectively identifies the virtual memory usage of executable(s), process(es), and shared libraries. Given a process ID, <span>memstat</span> can list the executable files, data, and shared libraries related to that process.
$ memstat -p <PID>
08. <span>nmon</span>
<span>nmon</span> is a system benchmarking tool based on <span>ncurses</span><span>, which can monitor the interactive patterns of CPU, memory, I/O, file system, and network resources. For memory usage, it can display real-time information about total/remaining memory, swap space, etc.</span>
$ nmon
09. <span>ps</span>
<span>ps</span> command can display the memory usage of each process in real-time.
Reported memory usage information includes %MEM (percent of physical memory used), VSZ (total amount of virtual memory used), and RSS (total amount of physical memory used).
You can use the “–sort” option to sort processes, for example by <span>RSS</span>:
$ ps aux --sort -rss
10. <span>smem</span>
<span>smem</span> command allows you to summarize memory usage of different processes and users based on <span>/proc</span> information. Memory usage analysis can be exported as charts (such as bar charts and pie charts).
>sudo smem --pie name -c "pss"
11. top
top command provides real-time resource usage statistics of running programs. You can sort by memory usage and size.
$ top -hv | -bcisSHM -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
Parameter descriptions:
- • -b: Run in batch mode. Usually used to send the output of top to other programs or save to a file;
- • -c: Display the command line of the executing task;
- • -d: Set delay time;
- • -h: Help;
- • -H: Show threads. When this setting is enabled, all threads generated by processes will be displayed;
- • -i: Show idle processes;
- • -n: Number of executions. Generally used with -b;
- • -u: Monitor processes related to a specified user;
- • -U: Monitor processes related to a specified user;
- • -p: Monitor specified processes. When monitoring multiple processes, process IDs are separated by commas. This option can only be used in the command line;
- • -s: Safe mode operation;
- • -S: Cumulative time mode;
- • -v: Display top version and then exit;
- • -M: Automatically display memory units (k/M/G).
Global Commands
- • Enter, Space: Refresh display information;
- • ?, h: Help;
- • =: Remove all task display limits;
- • A: Toggle alternating display mode;
- • B: Toggle bold display;
- • d, s: Change interface refresh time interval;
- • G: Select other window/column groups;
- • I: Toggle Irix or Solaris mode;
- • u, U: Monitor processes related to a specified user;
- • k: End process;
- • q: Exit top;
- • r: Reset the nice value of a process;
- • W: Save current settings;
- • Z: Change color template.
Summary Area Commands
- • l: Toggle average load and system uptime display;
- • m: Toggle memory and swap space usage display;
- • t: Toggle current tasks and CPU status display.
1: Summary display of CPU status or separate display of each CPU status.
Task Area Commands
Appearance Style
* b: Bold/Inverse color display of highlighted rows/columns. Controls the display style of x and y interactive commands
* x: Highlight sorted columns
* y: Highlight running tasks
* z: Color/black and white display.
Display Content
* c: Command line or process name of the task
* f, o: Add and remove process information columns and adjust the display order of process information columns
* H: Show threads
* S: Cumulative time mode
* u: Monitor processes related to a specified user
Number of Tasks Displayed
* i: Show idle processes
* n or #: Set maximum number of tasks displayed
Task Sorting (shift+f)
* M: Sort by memory usage
* N: Sort by PID
* P: Sort by CPU usage
* T: Sort by Time+
* <: Sort by the left adjacent column of the current sorting column
* >: Sort by the right adjacent column of the current sorting column
* F or O: Select sorting column
* R: Reverse sorting
12. <span>vmstat</span>
<span>vmstat</span> command displays real-time and average statistics covering CPU, memory, I/O, etc. For example, regarding memory, it displays not only physical memory but also virtual memory statistics. It can monitor the operating system’s process status, memory, virtual memory, disk I/O, context, and CPU information.
vmstat [-a] [-n] [-S unit] [delay [ count]]
- • -a: Display active and inactive memory;
- • -m: Display slabinfo;
- • -n: Only display field names once at the start;
- • -s: Display memory-related statistics and various system activity counts;
- • delay: Refresh time interval. If not specified, only one result is displayed;
- • count: Refresh count. If refresh count is not specified but refresh time interval is specified, the refresh count will be infinite;
- • -d: Display statistics related to each disk;
- • -S: Display using specified units. Parameters are k, K, m, M, representing 1000, 1024, 1000000, 1048576 bytes (byte). The default unit is K (1024 bytes);
- • -V: Display vmstat version information;
- • -p: Display statistics for specified disk partitions;
- • -D: Display overall disk information.
$ vmstat -s m 1 5
- • Procs
- • R: Number of processes waiting to be executed, indicating the number of processes running and waiting for CPU time slices.
- • B: Number of queued processes, indicating the number of processes waiting for resources.
- • Memory
- • Swap: Virtual memory, size of memory switched to virtual memory.
- • Free: Size of free physical memory.
- • Buff: Size of buffer.
- • Cache: Size of cache.
- • Swap
- • Si: Disk writes to virtual memory, size of memory entering virtual memory.
- • So: Virtual memory writes to disk, size of memory entering disk.
- • Io
- • Bi: Total amount of data read from block devices, reading from disk;
- • Bo: Total amount of data written to block devices, writing to disk.
- • System
- • In: Number of device interrupts per second;
- • Cs: Number of context switches per second.
- • Cpu
- • Us: Percentage of CPU consumed by user processes;
- • Sy: Percentage of CPU consumed by kernel processes;
- • Id: Percentage of time CPU is idle;
- • Wa: Percentage of time CPU is waiting for I/O.
Thank you all for your support and attention! Remember to share, like, and follow!Disclaimer:This article’s material is sourced from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact me for removal.