Debug School

rakesh kumar
rakesh kumar

Posted on

Command to check ram and cpu utilization in linux

The ps command is a Unix/Linux command used to display information about processes running on a system. Let's break down the given command step by step:

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Enter fullscreen mode Exit fullscreen mode

ps:

This is the command itself, used for displaying information about processes.
-eo pid,ppid,cmd,%mem,%cpu:

The -eo option 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).
| head:

The | (pipe) symbol is used to redirect the output of the ps command to another command, in this case, head.
head is used to display only the first few lines of the output (by default, the first 10 lines).
Let's consider an example to illustrate:

Suppose we have the following sample output 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
Enter fullscreen mode Exit fullscreen mode

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.

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:

Ensure 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.
For High Memory Utilization:
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.
Check for memory leaks and fix them.
Increase RAM:

If possible, consider upgrading your system's RAM to handle higher workloads.
Determine if your system's hardware meets the requirements of the tasks.
Use Lightweight Alternatives:

Consider using lightweight alternatives for resource-intensive applications.
Example: Lightweight desktop environments, text-based applications.
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.

CPU Utilization:
top:

Interactive command-line utility that provides real-time information about system processes and resource usage.

$ top
Enter fullscreen mode Exit fullscreen mode

htop:

An improved version of top with a more user-friendly interface.

$ htop
Enter fullscreen mode Exit fullscreen mode

ps:

Provides a snapshot of current processes, similar to the command you mentioned.

$ ps aux --sort=-%cpu | head
Enter fullscreen mode Exit fullscreen mode

mpstat:

Reports processors related statistics.

$ mpstat
Enter fullscreen mode Exit fullscreen mode

pidstat:

Reports statistics for Linux tasks.

$ pidstat 1
Enter fullscreen mode Exit fullscreen mode

Memory Utilization:
free:

Displays the amount of free and used memory in the system.

$ free -m
Enter fullscreen mode Exit fullscreen mode

vmstat:

Reports virtual memory statistics.

$ vmstat 1
Enter fullscreen mode Exit fullscreen mode

pmap:

Displays the memory map of a process or processes.

$ pmap <PID>
Enter fullscreen mode Exit fullscreen mode

smem:

Provides a report that includes swap usage along with memory usage.

$ smem -rs size
Enter fullscreen mode Exit fullscreen mode

htop:

Besides CPU information, htop also provides detailed memory usage.

$ htop
Enter fullscreen mode Exit fullscreen mode

ps:

Similar to the CPU command, you can use ps to show memory usage.

$ ps aux --sort=-%mem | head
Enter fullscreen mode Exit fullscreen mode

Top comments (0)