---
title: How to Monitor CPU and Memory Usage in Linux
description: Monitoring CPU and memory usage is essential for managing Linux systems effectively. Linux provides several commands to get a detailed look at resource consumption, helping administrators optimize performance and troubleshoot issues. Here are some essential commands for monitoring CPU and…
url: "https://truehost.com/support/knowledge-base/how-to-monitor-cpu-and-memory-usage-in-linux/"
updated: "2024-10-30"
category: Cloud servers in Kenya
---

# How to Monitor CPU and Memory Usage in Linux

Monitoring CPU and memory usage is essential for managing Linux systems effectively. Linux provides several commands to get a detailed look at resource consumption, helping administrators optimize performance and troubleshoot issues. Here are some essential commands for monitoring CPU and memory usage.

#### 1. **Using `top` Command**

The `top` command is a popular utility that provides a real-time view of CPU and memory usage.

- **To start `top`**:

```
top
```

- **Key Features**:
  - Shows CPU, memory, and swap usage.
  - Displays a list of processes sorted by CPU usage by default.
  - Press `M` to sort by memory usage. You may want to press shift+m

![](https://truehost.com/support/wp-content/uploads/2024/10/image-646.png)

#### 2. **Using `htop` Command**

`htop` is an improved version of `top` with a more user-friendly interface.

- **Install `htop`**:

```
sudo apt install htop # Debian/Ubuntu

sudo yum install htop # CentOS/RHEL
```

- Start **`htop`**:

```
htop
```

- **Features**:
  - Color-coded display for easy reading.
  - Ability to scroll vertically and horizontally.
  - Interactive interface to kill processes directly from the list.

![](https://truehost.com/support/wp-content/uploads/2024/10/image-647-1024x517.png)

#### 3. **Using `vmstat` Command**

`vmstat` (Virtual Memory Statistics) reports information about processes, memory, paging, block I/O, and CPU usage.

- **Run vmstat with a delay:**
  - This will refresh every 2 seconds.

```
vmstat 2
```

- **Key Columns**:
  - `r`: Number of processes waiting for CPU.
  - `free`: Amount of free memory.
  - `si` and `so`: Swap memory in and out.
  - `us`, `sy`, `id`: CPU time for user, system, and idle.

![](https://truehost.com/support/wp-content/uploads/2024/10/image-648-1024x397.png)

#### 4. **Using `free` Command**

The `free` command provides a quick overview of system memory usage, showing total, used, and available memory.

- **Run `free` with human-readable format**:

```
free -h
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-649-1024x175.png)

**5. Using `sar` Command**

`sysstat`’s `sar` command provides historical data on CPU, memory, and other resource usage over time. This is especially useful for identifying trends.

- **Install sysstat**

```
sudo apt install sysstat # Debian/Ubuntusudo yum install sysstat # CentOS/RHEL
```

- **Enable Data Collection**:
  - The `sysstat` service should be enabled and configured to collect data periodically.
  - **Edit the Configuration File**: Open the sysstat configuration file.
  - **Set `ENABLED` to `"true"`**: Change the `ENABLED` line to `true` so that `sysstat` collects and logs system statistics.
  - Save and close the file.
  - **Restart the `sysstat` Service**: After enabling data collection, restart the `sysstat` service to apply changes.

```
sudo vi /etc/default/sysstat
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-651.png)

```
sudo systemctl restart sysstat
```

You can then check the status

```
sudo systemctl status sysstat
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-652.png)

- **View CPU Usage over the past intervals**:
  - This example shows CPU usage at 1-second intervals, 5 times.

```
sar -u 1 5
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-650-1024x174.png)

- **View Memory Usage:**

```
sar -r
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-653-1024x173.png)

**6. Using `iostat` Command**

`iostat` is useful for monitoring CPU load and input/output (I/O) statistics on disks.

- **Install `sysstat`** (if not already installed):

```
sudo apt install sysstat # Debian/Ubuntusudo yum install sysstat # CentOS/RHEL
```

- **Run `iostat`**:
  - This command shows CPU statistics at 2-second intervals.

```
iostat -c 2
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-654.png)

7. **Using `ps` Command**

`ps` (Process Status) displays information about active processes and can be customized to show specific details.

- **View Processes by Memory Usage**:

```
ps aux --sort=-%mem | head
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-655-1024x236.png)

- **View Processes by CPU Usage**:

```
ps aux --sort=-%cpu | head
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-656-1024x244.png)

8. **Using `mpstat` Command**

`mpstat` (Multiprocessor Statistics) shows CPU usage per processor, which is valuable for multi-core systems.

- **Run `mpstat`**:
  - This will show statistics for each core on the system, helping to identify if any single core is overloaded.

```
mpstat -P ALL
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-658.png)

### Summary of Commands

- **General Resource Overview**: `top`, `htop`
- **CPU Usage**: `top`, `mpstat`, `sar -u`
- **Memory Usage**: `free`, `vmstat`, `sar -r`
- **Disk I/O**: `iostat`, `vmstat`

With these commands, Linux administrators have a toolkit for continuous monitoring and diagnostics to manage system resources effectively.
