Find top running CPU & Memory usage in Linux

memory

Memory and CPU usage in Linux refer to the utilization of system resources, specifically RAM (Random Access Memory) and CPU (Central Processing Unit), respectively.

Monitoring these metrics is essential for understanding the performance and health of a Linux system.

Memory Usage:

RAM (Random Access Memory): This is the volatile memory that the system uses to store data that is actively being used or processed by the CPU. It is faster than storage (like hard drives or SSDs) but is temporary and gets cleared when the system is powered off.

Linux uses a combination of free RAM and swap space (a designated area on disk) to manage memory.

To find the top running CPU and memory usage in a Linux web hosting environment, you can use various command-line tools or monitoring applications.

Memory Usage Tools:

The free command provides information about total, used, and free memory.

free -m

The top command displays real-time information about system resource usage, including memory.

top

The htop command is an enhanced version of top with a more user-friendly interface.

htop

The ps command can be used to list processes and sort them based on different criteria.

ps aux --sort=-%mem | head -n 11

CPU Usage:

The CPU is the brain of the computer, responsible for executing instructions and processing data. Monitoring CPU usage is crucial for understanding how much processing power is being utilized.

CPU Usage Tools:

CPU usage tools are software applications or commands in Linux that help users and system administrators monitor and analyze the utilization of the Central Processing Unit (CPU) in a computer system.

These tools provide insights into how much of the CPU’s processing power is being used by various processes and applications.

High CPU usage by a single program may indicate that it is highly demanding of processing power or that it may malfunction; for example, it has entered an infinite loop.

The top command also provides information about CPU usage in addition to memory usage.

top

The htop command shows CPU usage as well.

htop

The mpstat command provides detailed information about CPU usage.

mpstat

The sar command (System Activity Report) can be used for comprehensive system resource monitoring.

sar

It’s important to note that there are various graphical tools and system monitoring applications available that provide a more user-friendly interface for monitoring both memory and CPU usage. Additionally, monitoring tools may vary slightly depending on the Linux distribution in use.

The ps command can be used to list processes and sort by CPU usage

ps aux --sort=-%cpu | head -n 11

Check Top Processes sorted by RAM or CPU Usage in Linux

The following command will show the list of top processes ordered by RAM and CPU use in descendant form (remove the pipeline and head if you want to see the full list):

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

The -o (or –format) option of ps allows you to specify the output format. A favorite of mine is to show the processes’ PIDs (pid), PPIDs (pid), the name of the executable file associated with the process (cmd), and the RAM and CPU utilization (%mem and %cpu, respectively).

Additionally, I use –sort to sort by either %mem or %cpu. By default, the output will be sorted in ascendant form, but personally I prefer to reverse that order by adding a minus sign in front of the sort criteria.

To add other fields to the output, or change the sort criteria, refer to the OUTPUT FORMAT CONTROL section in the man page of ps command.