How to Use top and htop to Manage Memory on Cloud Servers?
Managing memory smartly is super important. This is especially true for applications or services on cloud servers.
A slow or an interruption may mean something is eating up memory or CPU without you noticing. Well, top and htop are simple command-line tools in Linux that can help in such situations. They show you which programs are using how much memory or CPU. Thus, you can find out what is going wrong and stop unnecessary programs. All in all, you can make your system faster in real-time.
On cloud servers, you need a fast way to keep track of performance. However, managing a remote server (like AWS, DigitalOcean, or Google Cloud does not come with the luxury of a visual desktop. top and htop are the best ways to manage memory at that time.
In this article, we will learn how to use these tools properly, one by one.
What exactly is top?
top is a command-line tool that shows live updates of your system activities. It mainly shows running processes, how much memory or CPU each one is using, and other details like uptime and load average.
Moreover, it runs directly in the terminal. The display updates every few seconds. You can interact with it and make changes to the view. Also, it is easy to, sort data or even kill processes. You need not leave the interface for any of this.
System admins can spot memory hogs and heavy processes quickly with top tool.
How to open top
Just open your terminal and type –
top
It starts showing system info in real-time. You will see something like this (not exact, but similar) –
top - 18 - 05 - 41 up 3 - 12, 2 users, load average - 1.22, 0.89, 0.70 Tasks - 210 total, 1 running, 209 sleeping, 0 stopped, 0 zombie %Cpu(s) - 8.0 us, 3.0 sy, 0.0 ni, 85.0 id, 1.0 wa, 0.0 hi, 3.0 si, 0.0 st MiB Mem - 7982.0 total, 3001.2 free, 2250.3 used, 2730.5 buff/cache
Lower half of the screen will have a long list of running processes with columns like PID, user, CPU usage, memory usage, and so on.
Breaking down what you see in top
Let’s slowly understand what each part of the output means.
First line
This shows –
- Current time
- How long the system has been on (uptime)
- How many users are logged in
- Load average for last 1, 5, and 15 minutes
Load average higher than the number of CPU cores means the system is overloaded.
Second line – Tasks
This line shows how many processes are running or idle. For example –
- Running means the process is active
- Sleeping means it is waiting for something
- Stopped is paused
- Zombie is already dead, but its parent process has not cleared it yet
Third line – CPU usage
This shows how CPU time is being used –
- us – user-level processes
- sy – system or kernel processes
- ni – nice value (priority level)
- id – idle time (more is good)
- wa – waiting for I/Ohi and si – for hardware/software interrupts
- st – steal time (in cloud VMs, it shows time stolen by other VMs on the same hardware)
Memory section
This shows your total memory and how much is –
- Free
- Used
- Buffers and cache
Also, you will see swap memory info. In case of a full RAM, Linux can push some data into swap space. However, a full swap will cause your server to slow down badly.
Process section (the big table)
This part lists all the processes currently running. Understand each column below –
- PID – Process ID
- USER – Who started it
- PR – Priority (lower is higher priority)
- NI – Niceness (0 is default)VIRT – Virtual memory used
- RES – Actual RAM used
- SHR – Shared memory
- S – State (R for running, S for sleeping, etc.)
- %CPU – CPU usage by that process
- %MEM – Memory usage
- TIME+ – Total CPU time since it started
- COMMAND – Name of the process
Useful things you can do inside top
top is highly interactive. That means you can press keys to do different activities. We will through the common ones here –
Change refresh rate
By default, top refreshes every 3 seconds. To change this –
- Press d
- Type new interval (e.g., 1) and hit Enter
Kill a process
You can kill a process if it eats too much memory or is stuck –
- Press k
- It will ask for the PID. Enter PID and press Enter
- Next, it asks for signal (default is 15 for graceful kill). You can use 9 for force kill.
Switch memory units
To change units like KiB, MiB, GiB –
- Press E for summary section
- Press e for process list
Filter processes
To show only certain types of processes –
- Press o or O
- Type a filter like COMMAND=somescript, !COMMAND=somescript (if you do not have somescript), %CPU>5.0, etc.
Press = to remove filters
Sort the process list
You can change the sorting like this –
- P – by CPU usage
- M – by memory
- T – by total time
- N – by PID
You can get a full list of fields –
- Press F and enter interactive menu
- Move up and down with arrow keys
- Press s to select one
- Press Enter to apply
Press R and reverse the sorting order (ascending (AESC)/descending (DESC)).
Show the processes of one user only
This views only processes started by a specific user –
- Press u
- Enter the username and press Enter
Or directly launch like this –
top -u username
Changing Priority of Running Programs (Renice with top)
Suppose one program is using too much of your system power and you want to reduce its priority. Or maybe your program is very important and you want it to run faster than others. For that, you can change its nice value using the top command.
- Press the letter r.
- It will ask you to enter the PID (process ID) of the program whose priority you want to change. Type the correct PID and press Enter.
- Then it will ask you to give a new nice value. You can give a number from -20 to +19. A lower number means higher priority. So, -20 means the system will try to run that process faster. +19 means it will become less important and run slower than others.
Changing CPU and Memory Views in top
You can switch to different views for the top very easily.
Press t if you want to change how CPU information is displayed.
Press m to switch the memory usage style.
Other Useful Keys While Using top
- Press H to change the view from processes to threads. Threads are like small parts of a program.
top -H
- Press c to see the full location of the running programs.
top -c
- To see which process is started by which parent process, press v or V. This will show you a tree-like structure. It is called forest view.
- Press the spacebar to refresh immediately if the screen is not updating fast enough.
Saving Your Setup for Next Time
Press W while top is running. It will save the current setup to a file called .toprc in your home folder.
Also, next time when you run top, it will use your saved settings automatically.
What is htop and why use it?
htop is a terminal-based process viewer just like top, but it gives a real-time view of all the running processes and system usage in a more organised and colourful format. top might feel a bit old-school, but htop adds that modern touch. It helps a great deal to find performance issues. You can scroll up and down using the arrow keys to see all processes. You can even use the mouse!
How to install and run htop
Many Linux distros don’t have htop by default, but it is easy to install.
Open your terminal and run the command based on your OS –
- For Ubuntu or Debian-based systems –
sudo apt install htop
- For RHEL, CentOS, or Fedora –
sudo dnf install htop
- For Arch Linux –
sudo pacman -S htop
Then, type this to launch –
htop
Understanding the htop Interface
When htop opens up, the screen is divided into two sections.
Top Section – System Overview
This part gives you a bird ’s-eye view of what’s happening on your system.
- CPU usage with separate bars for each core.
- Memory and swap usage.
- Load averages (1, 5, 15 minutes).
- Uptime.
- Number of running tasks and threads.
Bottom Section – Process List
Here you will see a live list of processes similar to top. Each row is a process, and each column shows different details like –
- PID
- User
- CPU%
- MEM%
- Time
- Command
Navigating htop with keyboard
- Use F3 to search for a process.
- Press F4 to filter processes.
- F6 lets you change the column used for sorting (like CPU or MEM).
- Press F9 to kill a process (you can choose the signal to send).
- F2 opens the setup menu to tweak how the interface looks and works.
- Use arrow keys to move around.
Sorting and Filtering Processes in htop
This makes it super simple to isolate heavy tasks, rogue processes, or just things running under your user.
- Hit F6 and select %CPU or %MEM to find top resource consumers.
- Use F4 and type a keyword to show only matching processes.
- Want to see processes of a particular user? Press u, then enter the username.
Killing or Managing Processes
- Select the process using arrow keys.
- Press F9 to bring up the kill menu.
- Choose a signal to send. For example, 9 for SIGKILL or 15 for SIGTERM.
Renice a process (change its priority) –
- Select it, then press F7 to increase priority (lower nice value).
- Press F8 to decrease priority (higher nice value).
Remember – You need root access for changing the priority of other users’ processes.
Conclusion
Both top and htop are powerful, but htop is ideal if –
- You want a visual layout and prefer interactive control.
- You are dealing with too many processes on cloud servers.
- You need quick sorting, filtering, and killing of tasks.