How to Check CPU and Memory in Linux Command: 7 Essential Methods

If you’re managing a Linux system, knowing how to check CPU and memory in Linux command is vital for ensuring optimal performance and diagnosing potential issues. In this comprehensive guide, we’ll explore seven essential methods to check your CPU and memory usage effectively. This will equip you with the necessary tools to monitor your system resources, enabling efficient management and troubleshooting.

How to Check CPU and Memory in Linux


Why Monitoring CPU and Memory is Important

Monitoring CPU and memory usage is crucial for several reasons:

  • Performance Tuning: Identifying high resource usage can help in optimizing applications and processes.
  • System Stability: Regular checks can prevent system crashes or slowdowns caused by resource exhaustion.
  • Capacity Planning: Understanding resource consumption helps in planning for upgrades and scaling.

How to Check CPU and Memory in Linux Command

Method 1: Using the top Command

The top command provides a real-time, dynamic view of system processes and their CPU and memory usage.

Command:

top

Output Example:

top - 15:45:25 up 1 day,  3:25,  1 user,  load average: 0.14, 0.25, 0.30
Tasks: 206 total,   1 running, 205 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  0.5 sy,  0.0 ni, 96.0 id,  1.5 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7972.1 total,   1591.4 free,   3920.3 used,   3460.4 buff/cache
MiB Swap:   2048.0 total,   1630.2 free,    417.8 used.   4418.8 avail Mem

Method 2: Using the htop Command

htop is an interactive process viewer with a user-friendly interface, displaying CPU and memory usage clearly.

Command:

htop

Output Example:

In htop, you’ll see CPU usage represented as colored bars, memory usage displayed in percentage, and processes listed in real-time. (Note: You may need to install htop using your package manager.)

Method 3: Checking Memory Usage with free

The free command provides a quick summary of memory usage in the system.

Command:

free -h

Output Example:

              total        used        free      shared  buff/cache   available
Mem:      7.8Gi       3.8Gi       1.5Gi       200Mi       2.5Gi       3.5Gi
Swap:      2.0Gi       1.0Gi       1.0Gi

Method 4: Using the vmstat Command

The vmstat command reports virtual memory statistics, including CPU and memory usage.

Command:

vmstat 1

Output Example:

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b    swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 1  0       0  312504  15024  185832    0    0     0     0  102  172  2  1 97  0

Method 5: Using the mpstat Command

The mpstat command displays CPU usage for each processor, which is useful for multi-core systems.

Command:

mpstat -P ALL 1

Output Example:

Linux 4.15.0-96-generic (hostname)     10/22/2024  _x86_64_    (4 CPU)
15:45:01 PM  CPU   %usr   %nice %sys %iowait %irq %soft %steal %guest %gnice  %idle
15:45:01 PM  all   1.50   0.00  0.50  0.00  0.00  0.00  0.00  0.00  0.00 98.00

Method 6: Using sar for Historical Data

The sar command collects and reports system activity information, including CPU and memory usage over time.

Command:

sar -u 1 3

Output Example:

Linux 4.15.0-96-generic (hostname)     10/22/2024  _x86_64_
15:45:01 PM  CPU     %user   %nice %system %iowait  %steal %idle
15:45:02 PM  all      2.00    0.00    0.50    0.00    0.00  97.50
15:45:03 PM  all      1.50    0.00    0.60    0.00    0.00  97.90

Method 7: Using the /proc Filesystem

The /proc filesystem contains a wealth of information about the system, including CPU and memory statistics.

Command for CPU Info:

cat /proc/cpuinfo

Command for Memory Info:

cat /proc/meminfo

Output Example for CPU Info:

processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 58
model name    : Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz

Output Example for Memory Info:

MemTotal:       7972 MB
MemFree:        1591 MB
MemAvailable:   3460 MB
Buffers:        15024 kB
Cached:         185832 kB

Conclusion

Monitoring CPU and memory in Linux systems is crucial for effective resource management and system optimization. By employing the methods discussed in this guide, you can easily check and analyze your CPU and memory usage, leading to better performance and stability.

Remember, for more detailed insights and tips on Linux and DevOps tools, feel free to explore Geekers Hub.


FAQs

What is the best command to check CPU and memory usage in real-time?

The top or htop commands are the best options for real-time monitoring of CPU and memory usage.

How can I get a summary of memory usage quickly?

The free -h command provides a quick summary of memory usage, including total, used, and free memory.

Is there a graphical tool for monitoring CPU and memory?

Yes, tools like GNOME System Monitor and KSysGuard offer graphical interfaces for monitoring system resources.

Can I check CPU and memory usage on a remote server?

Yes, you can use SSH to connect to a remote server and execute the commands discussed in this guide.

What should I do if my CPU or memory usage is consistently high?

Consider optimizing your applications, checking for resource-intensive processes, or upgrading your hardware if necessary.

External Resources