Monitoring Linux Memory Usage: Tracking A Process

how to monitor memory usage of a process in linux

There are several ways to monitor the memory usage of a process in Linux. Here are some of the most common methods:

- Using the `ps` command: This command provides detailed information about running processes, including memory usage. You can use the `-e` option to show all processes, regardless of the user, and the `-o` option to specify the output format, such as memory usage.

- Using the `top` command: This command provides a dynamic, real-time view of processes and their memory usage. You can use the `-U` option to display processes for a specific user, and press `Shift + M` to sort processes by memory usage.

- Using the `pmap` command: This command reports the memory map of a process, including the size, permissions, and mapping location. It provides detailed information about each memory mapping, such as size, resident set size (RSS), shared, and private memory usage.

- Using the `/proc/[PID]/` directory: This directory provides detailed information about a specific process, including memory usage. You can obtain memory usage information from files such as `/proc/[PID]/statm` and `/proc/[PID]/status`.

- Using GUI tools: Graphical interfaces like GNOME System Monitor, KDE System Monitor, and Xfce Task Manager provide a user-friendly way to monitor memory usage.

- Using third-party tools: Tools like Nagios, Zabbix, and Cacti offer advanced memory monitoring capabilities, including alerting, trend analysis, and historical data.

Characteristics Values
Command-line tools ps, top, htop, pmap, smem
GUI tools GNOME System Monitor, KDE System Monitor, Xfce Task Manager
Third-party tools Nagios, Zabbix, Cacti
Important Terminologies VSZ, RSS, USS, PSS

shundigital

Use the top command to monitor one process's memory/CPU usage in real-time

The top command can be used to monitor the memory and CPU usage of a single process in real-time. The command is as follows:

`top -p

Here, `` is the process ID of the process you want to monitor.

To exit the top command, you can use Q or CTRL+C.

The top command provides a dynamic real-time overview of what's happening on your server. It displays the following:

  • System time, server uptime, and user sessions
  • System tasks breakdown
  • Real-time CPU usage broken down into categories
  • Real-time memory usage
  • A list of active processes sorted by heaviest CPU usage

You can also use the following top commands to dive deeper into issues:

  • Show the full path of your processes: `c`
  • Display the processes of a specific user: `top -U `
  • Change the screen reset interval: `d`
  • Kill a process without exiting top: `k`
  • Re-order by highest CPU usage: `SHIFT+P`

shundigital

Use the ps command to check memory usage by process

The `ps command can be used to check memory usage by process. The `ps` command provides information about the currently running processes, including their process identification numbers (PIDs), USER, TTY, %CPU, %MEM and command. Every process is assigned a unique ID called PID. The %CPU shows the percentage of CPU usage of the process and %MEM shows the percentage of memory usage of each running process in the system.

To check the memory usage for all processes, the following `ps` command will display %MEM in the first column, PID in the second column and command in the third column for all running processes on the system:

`$ ps -eo pmem,pid,cmd | sort -k 1 -nr`

To display the top 5 processes by memory usage, enter the following `ps` command:

`$ ps -eo pmem,pid,cmd | sort -k 1 -nr | head -5`

The `ps` command can also be used to sort output by memory (RAM), from high to low. The following command can be used:

`$ ps --sort -rss`

The `ps` command can be used in combination with other commands to provide more detailed information about memory usage. For example, the following command can be used to get the total memory used by the various processes running on the Linux machine in MB:

`$ ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | awk '{total=total + $1} END {print total}`

shundigital

Use the pmap command to get a detailed breakdown of memory usage

The pmap command can be used to get a detailed breakdown of memory usage. The pmap command reports the memory map of a process or processes.

To display process mappings, type:

Bash

$ pmap pid

The pmap output reports the process' memory usage, including all the components it uses, such as libraries and binary files. The columns include the memory address, offset, permission, and name.

The flag can be used to get even more detailed output:

Bash

$ pmap -x pid

The output of the above command will include the following information:

  • Address: The beginning memory address allocation
  • Kbytes: Memory allocation in kilobytes
  • RSS: Resident set size of the process in memory
  • Dirty: The status of the memory pages
  • Mode: Access mode and privileges
  • Mapping: The user-facing name of the application or library

The pmap utility gathers most of its information from the /proc/PID/smaps file and makes it human-friendly.

You can use pmap to monitor the movement of memory across a particular time range, to analyze a memory value that always increases and never decreases, or to locate the culprit of a memory leak.

shundigital

Use the /proc/[PID/ directory to get memory usage information for a specific process

The /proc/[PID] directory contains files and subdirectories exposing information about the process with the corresponding process ID.

To get memory usage information for a specific process, you can use the following commands:

  • Cat /proc/[PID]/status: This file contains information about the process's memory usage, such as VmPeak, VmSize, VmLck, VmHWM, VmRSS, VmData, VmStk, VmExe, and VmLib.
  • Cat /proc/[PID]/statm: This file provides a more detailed breakdown of memory usage, including the total program size, resident set size, shared pages, text size, library size, and more.
  • Pmap: This command displays the memory map of a process, including the address, permissions, offset, device, inode, and size of each memory segment.
  • Smaps: This command provides a detailed breakdown of memory usage by process, including private and shared memory usage.
  • Ps: The ps command includes a column that displays memory usage for each process. You can use the --sort option to sort the output by memory usage.
  • Top: The top command allows you to monitor memory usage for a specific process by providing the PID or process name. You can also press "shift+m" to order the processes by memory usage.

shundigital

Use the smem tool to get memory usage information for processes, including PSS and USS metrics

The smem tool is a command-line memory reporting tool that provides diverse reports on memory usage on a Linux system. It is unique compared to other traditional memory reporting tools as it reports the Proportional Set Size (PSS), which is a more meaningful representation of memory usage by applications and libraries in a virtual memory setup.

To install smem on Linux, use the following command:

Bash

Sudo apt install smem

Once installed, you can run the following command to view a report of memory usage across the whole system, by all system users:

Bash

Smem

When a normal user runs smem, it displays memory usage by a process that the user has started, with the processes arranged in order of increasing PSS.

To view system-wide memory consumption, use the following command:

Bash

Smem -t

To view memory usage on a per-user basis, run the following command:

Bash

Smem -u

You can also report memory usage statistics by mappings as follows:

Bash

Smem -m

To filter the output by process name, use the -P or --processfilter="regex" option:

Bash

Sudo smem --processfilter="chrome"

Smem also supports graphical output in the form of pie charts and bar graphs. For example, to generate a pie chart showing memory usage (USS, PSS, and RSS) for processes owned by the root user, use the following command:

Bash

Sudo smem --userfilter="root" --pie name -s pss

Smem provides a more accurate representation of memory usage compared to other tools like top and htop, which primarily display the Resident Set Size (RSS). smem's PSS metric takes into account shared memory pages and provides a better understanding of how much memory would be freed if a particular process were terminated.

Frequently asked questions

There are several ways to monitor the memory usage of a process in Linux, including command-line tools, graphical user interfaces (GUIs), and third-party tools. Here are some of the most commonly used methods:

- Using the ps command: The 'ps' command displays information about running processes, including memory usage. You can use options like '-e' to show all processes and '-o' to specify the output format, such as memory usage.

- Using the top command: The 'top' command provides a real-time view of system performance, including memory usage per process. You can sort processes by memory usage with the '-o %MEM -c' option or press 'Shift+M' while 'top' is running.

- Using the pmap command: The 'pmap' command displays detailed information about a process's memory mappings, including size, permissions, and mapping location. It provides both RSS (Resident Set Size) and PSS (Proportional Set Size) values, which represent the amount of memory used by a process.

- Manually checking the /proc//directory: The /proc//directory contains files that provide detailed information about a specific process, including memory usage. The /proc//statm file shows memory usage in terms of memory pages, and the /proc//status file contains human-readable information, including the Resident Set Size (RSS).

- Using third-party tools: Tools like smem, Nagios, Zabbix, and Cacti provide advanced memory monitoring capabilities, such as alerting, trend analysis, and historical data. These tools are often used in enterprise environments to manage multiple servers and applications.

Here are some key terminologies related to memory usage in Linux:

- VSZ (Virtual Memory Size): Refers to the total amount of virtual memory used by a process, including both the portion in RAM and the portion swapped out to disk, as well as any shared libraries.

- RSS (Resident Set Size): Refers to the portion of a process's memory that is held in RAM, including both the code and data segments. It does not include memory that has been swapped out to disk.

- Uss (Unique Set Size): Represents the amount of memory that is unique to a process and is not shared with any other processes. It is the amount of memory that would be freed if the process were to terminate.

- Pss (Proportional Set Size): Splits the accounting of shared pages between all processes that have them mapped. It provides an accurate representation of memory usage across multiple processes by avoiding double-counting shared memory.

To monitor memory usage by a specific user, you can use commands like 'top' with the '-U' option to focus on a single user, or 'ps' in combination with other commands to collect and sort memory usage data for each user. You can also use scripts to calculate and display memory usage totals for each user.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment