
Efficient memory management is crucial for maintaining the performance of cloud servers. This guide explores how to use top and htop to manage memory on cloud servers, providing clear steps to monitor system resources, analyze htop memory usage, and optimize performance. Whether you're troubleshooting issues or ensuring your server runs smoothly, these powerful Linux utilities are essential tools for effective memory monitoring. Before you begin, make sure your Linux system has the top utility installed.
top (table of processes) is a command-line utility on Linux and other Unix-like operating systems that displays memory utilization and memory usage. It provides a dynamic real-time view of an active system. The top utility also displays system summary information, including the total number of tasks, percentage of CPU usage, and a list of processes or threads currently being managed by the Linux kernel.
The types of system summary information shown and the types, order, and size of information displayed for processes are all user-configurable, and that configuration can be made persistent across restarts.
By default, top outputs an ordered list of running processes based on CPU usage and shows the top CPU consumers, but the ordering criteria can be changed based on user needs. This list is updated periodically every 3 seconds by default, but the refresh period can be changed.
top is a handy tool for system administrators, as it shows useful information like what users and processes are consuming the most system resources at a given time.
To run the top utility, enter the top command into the terminal:
This starts an interactive terminal session. You should get an output similar to this:
The upper half of the output contains statistics on resource usage and processes, while the lower half contains a list of running processes.
Let's break down the output, bit by bit. The first line shows the current timestamp (17:57:19), followed by the system uptime (i.e how long the system has been running - in this case, 2hrs and 49mins).
Next is the number of active user sessions (3), followed by the system's load average over the last 1, 5, and 15 minutes. Load refers to a measure of the amount of computational work the system performs. The load average is divided over 100, here the load average of our system in the last one minute is 1.39, which means the system was doing 139% of the available capacity. In this case, the system was a bit overloaded.
The line shown above displays the total number of running tasks or processes. We have a total of 246 running tasks with 1 running, 245 sleeping, 0 stopped, and 0 zombies.
The next line shows CPU usage in percentage. This tells how much CPU time is spent on various tasks.
The symbols have the following meaning:
These lines represent physical memory (RAM) usage in mebibytes(MiB):
The first line shows our total memory usage, free, used, and cached memory, while the second displays similar information about swap memory.
The columns in the second half of the output display specific information about processes.
Now that we've gone through the output let's dive into managing processes.
Note:
topis case-sensitive;randRhave two different effects.
By default, top has a refresh interval of 3 seconds. To change this interval - press d; you will be prompted for the new interval to set in seconds:
To kill a process, press k while top is running, you'll be presented a prompt like this within top:
Enter the PID of the process you wish to kill; let's kill firefox (PID=2387) from our top:
Next, you'll be prompted to enter the kill signal to be sent to the process. By default, top sends a SIGTERM, which allows processes to be terminated gracefully. You could type in SIGKILL or another signal you wish to send; you could also enter a signal number here. For instance, SIGKILL has a signal number of 9, and SIGTERM is 15.
In our example, memory usage is given in mebibytes (MiB):
We can cycle between displaying memory values in kibibytes (KiB), mebibytes (MiB), gibibytes (GiB), tebibytes (TiB), pebibytes (PiB), or exibibytes (EiB) by using E to switch within top.
To switch between similar memory units for the process list, an e is used.
The output might feel too cluttered, especially when you want to monitor just a bunch of similarly named processes or a single process. You can add filters to focus on just a few processes.
To use filters, enter o or O in top; a prompt will appear, and you can add a filter expression here:
A filter expression specifies a relation between an attribute and a value. Examples of filter expressions are:
You can add more filters even if you've already used one; this helps to further trim down to your desired result. To remove any filters you have added, enter = .
You can sort the top's process list by CPU utilization, memory use, etc.
Press P, and it will be sorted according to CPU utilization percentage in descending order, with processes with more CPU utilization listed first.
You can also press M, to sort by memory usage; T to sort by the TIME+ column, and N to sort by the PID column.
top also allows you to sort by other different attributes like VIRT, PR, NI. To select from a list of attributes to sort by, follow these steps while in top:
F to enter the interactive menu.s to select your choice.q to exit the interactive menu.This will order the process list based on your selected attribute. By default, top sorts in DESC (descending order), to switch between AESC (ascending order) and DESC; press R.
Alternatively, you can launch top with the o flag followed by the attribute to sort by, like "top -o attribute". For example, to sort by %CPU; we can launch top like this:
You can view processes for a specific user by pressing u, this will prompt for the user name of UID:
Enter the user name or UID and hit enter. Alternatively, you can launch top with the u flag followed by the user name:
top allows you to renice a process by pressing r; it then prompts you for the PID of the process you wish to renice:
Enter the process ID and enter, you'll be prompted for the new nice value to assign to the process. Nice values range from -20 to +19.
You can switch top's default way of showing CPU and memory statistics. top allows cycling between four different views. To switch between different CPU and memory views, press t and m, respectively.
Pressing these keys have the following effect on top's display:
H - switch tops display to threads instead of processes. Alternatively, you can launch top with the display set to threads by default with the H flag.
c - shows the full path to the program in the COMMAND column. Alternatively, you can launch top with the c flag.
v or V - switches to forest view, which shows a child-parent hierarchy of processes.
Space - this forces top to refresh its display when pressed.
To save any changes made to top's output for use later on, pressing W writes the configuration to the .toprc file in your home directory.
htop is an interactive process viewer and process manager like top, but it is not installed on most Linux distributions by default - unlike top.
To install htop:
While top focuses on the processes that consume the most system resources, htop shows all running processes.
htop also provides a rich-colored display and visual information about processor, swap, and memory status. It also provides a cursor-controlled interface for sending signals to processes. htop includes similar information as top, such as same columns information in the process list, but ties behavior to function keys rather than key presses used in top. To perform an action on a process, navigate to the process using the arrow keys and clicking the following function keys:
htop has similar functionality to top, but many system administrators prefer htop over top, due to its rich display and ease of use.
In this article, we have covered managing memory usage on Linux using top and htop. To learn more check out top and htop.
0 Comments
Be the first to comment and share your perspective with the community.