Debug School

Cover image for Resource utilization in Linux
Suyash Sambhare
Suyash Sambhare

Posted on • Updated on

Resource utilization in Linux

The ps command is a Unix/Linux command used to display information about processes running on a system.

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

  • ps: this is the command itself, used for displaying information about processes.
  • -eo: specifies the format of the output. It instructs ps to display specific information about each process.
  • pid: Process ID
  • ppid: Parent Process ID
  • cmd: Command that started the process
  • %mem: Percentage of physical memory (RAM) used by the process
  • %cpu: Percentage of CPU usage by the process
  • --sort=-%mem - The --sort= option is used to sort the output based on a specified column. Here, it sorts the output based on the %mem column in descending order (- indicates descending).

Examples:

The following sample output is from the ps command without sorting:

  PID  PPID CMD            %MEM  %CPU
  123    1   /usr/bin/foo   2.5   10.2
  456  123   /usr/bin/bar   1.8    5.5
  789    1   /usr/bin/baz   3.1    8.0
  ...
Enter fullscreen mode Exit fullscreen mode

Now, applying the given ps command:

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
The output would be:
  PID  PPID CMD            %MEM  %CPU
  789    1   /usr/bin/baz   3.1    8.0
  123    1   /usr/bin/foo   2.5   10.2
  456  123   /usr/bin/bar   1.8    5.5
  ...
Enter fullscreen mode Exit fullscreen mode

This result shows the top processes sorted by %mem in descending order, and head limits the display to the first few lines.

Linux

To find out and decrease CPU or memory utilization on a system, you can employ various strategies and solutions. Here are several approaches you can take:

  • For High CPU Utilization: Identify Resource-Intensive Processes:
  • Use system monitoring tools (top, htop, ps, etc.) to identify processes consuming the most CPU. Example: top or htop command.
  • Kill or Prioritize Processes: Terminate unnecessary or resource-intensive processes. Example: kill -9 PID to forcefully kill a process.
  • Optimize Code and Applications: Optimize resource-heavy applications or code to reduce CPU usage.
  • Identify and fix inefficient algorithms or code snippets.
  • Update Software: nsure that your system and applications are up-to-date with the latest patches and updates. Outdated software may have performance improvements in newer versions.
  • Check for Malware: Run a malware scan to ensure that your system is not infected, as malware can cause high CPU usage. Use antivirus tools to perform a thorough scan.
  • Adjust Process Priority: Adjust the priority of processes to manage CPU allocation better. Example: nice and renice commands to modify process priority.
  • Use CPU Affinity: Assign specific CPU cores to critical processes to distribute the load evenly. Example: taskset command.
  • Identify Memory-Hungry Processes: Use tools like top, htop, or ps to identify processes consuming the most memory. Example: top -o %MEM to sort by memory usage.
  • Terminate Unused Processes: Close or terminate processes that are not essential to free up memory. Example: Use the kill command.
  • Optimize Applications: Optimize applications to use memory efficiently.
  • Adjust Swappiness: Adjust the swappiness parameter to control how aggressively the system swaps out data to the swap space. Example: Modify the /etc/sysctl.conf or /etc/sysctl.d/ configuration files.
  • Check for Memory Leaks: Identify and fix memory leaks in applications to prevent gradual memory consumption. Use tools like Valgrind or specialized debugging tools.
  • Enable Compressed Memory: Enable kernel features like compressed memory to reduce memory usage. Example: Check if zswap or zram is supported and configure accordingly.

Remember that the effectiveness of these solutions may vary based on your specific system, applications, and workload. Regular monitoring and proactive management are essential to maintaining optimal system performance.

Commands

CPU Utilization:

  • top - Interactive command-line utility that provides real-time information about system processes and resource usage.
  • htop - An improved version of top with a more user-friendly interface.
  • ps - Provides a snapshot of current processes, similar to the command you mentioned. Eg. ps aux --sort=-%cpu | head
  • mpstat - Reports processors related statistics.
  • pidstat - Reports statistics for Linux tasks. Eg. pidstat 1

Memory Utilization:

  • free - Displays the amount of free and used memory in the system. Eg. free -m
  • vmstat - Reports virtual memory statistics. Eg. vmstat 1
  • pmap - Displays the memory map of a process or processes. Eg. pmap 7
  • smem - Provides a report that includes swap usage along with memory usage. Eg. smem -rs size
  • htop - Besides CPU information, htop also provides detailed memory usage.
  • ps - Similar to the CPU command, you can use ps to show memory usage. Eg. ps aux --sort=-%mem | head

Top comments (0)