Understanding Memory Usage with `smem`

Understanding Memory Usage with `smem`

Memory management is crucial for Linux administrators and developers, especially when optimizing performance for resource-intensive applications. While tools like top and htop are commonly used to monitor system performance, they often don’t provide enough detail regarding memory usage breakdown. This is where smem comes into play.

What is smem?

smem is a command-line tool that reports memory usage per process and provides better insight into shared memory than most traditional tools, taking shared memory pages into account. Unlike top or htop, which primarily display RSS (Resident Set Size), smem can also show USS (Unique Set Size), which is a better metric for understanding how much memory would be freed if a particular process were terminated. This blog will guide you through using smem, explaining these critical memory metrics and providing comparisons to more familiar tools.

Understanding Memory Metrics: RSS vs. USS

Before diving into smem usage, it’s important to understand two key memory metrics:

  • Resident Set Size (RSS): RSS represents the portion of a process’s memory that is held in RAM. This includes memory shared with other processes (e.g., shared libraries). Tools like top or htop display RSS, which gives a general sense of how much memory a process is using, but it doesn’t differentiate between shared and exclusive memory.
  • Unique Set Size (USS): USS measures the amount of memory that is used exclusively by a process, without considering shared memory. This is important because USS indicates how much memory would actually be released if the process were terminated. It provides a more accurate reflection of a process’s individual memory footprint.
  • Proportional Set Size(PSS): The unshared memory (USS) plus a process’s proportion of shared memory is reported as the PSS (Proportional Set Size). The USS and PSS only include physical memory usage. They do not include memory that has been swapped out to disk.

Installing smem

To install smem on Debian, Ubuntu, and Mint, you can use the following command:

sudo apt install smem

Once installed, you are ready to start analyzing memory usage.

Basic Usage of smem

The simplest way to use smem is to run it without any options. This provides an overview of the memory usage by all processes, displaying important metrics such as PID (Process ID), User, Command, RSS, PSS (Proportional Set Size), and USS.

smem

Output example:

  PID User     Command                         Swap     USS      PSS      RSS
  726 root     /usr/libexec/upowerd               0   11984    13736    25312
 1432 user     /usr/libexec/tracker-miner         0   10840    12934    23764

In the output:

  • USS (Unique Set Size): Shows the unique memory used by the process.
  • PSS (Proportional Set Size): An estimate of how much memory the process is using, considering shared memory divided proportionally across processes.
  • RSS (Resident Set Size): Shows how much memory the process has in RAM.

Viewing USS for All Processes

You can sort the output by USS to see which processes are using the most unique memory:

smem -s uss

Output example:

$ smem -s uss
    PID User    Command                          Swap     USS      PSS      RSS 
   1882 user    -bash                              0     2044     2501     5304 
   1783 user    /lib/systemd/systemd --user        0     3052     4359     9644 
1575618 user    /usr/bin/python3 /usr/bin/s        0     8684     9822    14128 

This command will display the processes sorted by USS, allowing you to easily identify memory hogs.

Viewing Memory Usage as Percentages

Sometimes, it’s helpful to view memory usage as a percentage of total system memory. To do this, use the -p flag:

smem -p

Output example:

$ smem -p
    PID User    Command                         Swap      USS      PSS      RSS 
   1882 user    -bash                          0.00%    0.03%    0.03%    0.07% 
   1783 user    /lib/systemd/systemd --user    0.00%    0.04%    0.05%    0.12% 
1732035 user    /usr/bin/python3 /usr/bin/s    0.00%    0.11%    0.12%    0.17% 

This will include percentage columns for USS, PSS, and RSS, making it easier to understand how much of your system’s memory is being consumed by individual processes.

Viewing USS for a Specific Process

If you want to drill down into the memory usage of a specific process, use the -P flag with a process ID (PID). This allows you to view USS, PSS, and RSS for that particular process.

smem -P <PID>

Example:

$ smem -P 1732035
    PID User     Command                         Swap      USS      PSS      RSS 
1944111 user     /usr/bin/python3 /usr/bin/s        0     8032     9120    13328 

This will show detailed memory information for the process with PID 1432.

Advanced Options

smem comes with several advanced options that allow further customization of the output:

  • Show Totals: You can use the -t flag to show the total memory usage for all processes.
  smem -t

Output example:

  $ smem -t
    PID User     Command                         Swap      USS      PSS      RSS 
   1882 user    -bash                              0     2044     2497     5304 
   1783 user    /lib/systemd/systemd --user        0     3052     4360     9644 
2025476 user    /usr/bin/python3 /usr/bin/s        0     8556     9675    13900 
-------------------------------------------------------------------------------
      3 1                                           0    13652    16532    28848 
  • Filter by User: To filter memory usage by a specific user, use the -U option followed by the username:
  smem -U user
  • Graphs: For a visual representation of memory usage, smem also supports pie charts and bar graphs. For instance:
  smem --pie uss
  smem --bar uss

Supported options to generate the charts are:

command process command line maps total number of mappings name name of process pid process ID pss proportional set size (including sharing) rss resident set size (ignoring sharing) swap amount of swap space consumed (ignoring sharing) user owner of process uss unique set size vss virtual set size (total virtual memory mapped)

Comparing smem to Other Tools

While tools like top, htop, and ps provide a good overview of system performance and memory usage, they lack the ability to distinguish between shared and unique memory effectively.

  • top and htop: These tools display RSS but don’t differentiate between memory that is shared across processes and memory that is uniquely used by a process. This makes them less precise for detailed memory analysis, especially in environments where shared libraries are prevalent.

  • ps: The ps command can display RSS but, similar to top, lacks insight into how memory is shared or unique to processes.

Conclusion

For Linux administrators and developers, understanding how memory is being used by individual processes is crucial, especially when optimizing applications or troubleshooting memory leaks. Tools like smem provide a deeper level of insight than more common utilities like top or htop. By focusing on USS, administrators can make more informed decisions about memory optimization, knowing exactly how much memory would be freed when terminating a process.

Whether you are investigating an application’s memory usage or trying to pinpoint inefficiencies in your system, smem gives you the detail you need to make data-driven decisions.

Resolving commands 'Killed' on GCP f1-micro Compute Engine instances

Resolving commands 'Killed' on GCP f1-micro Compute Engine instances

When I want to perform a quick task, I generally spin up a Google GCP Compute Engine instance as they’re cheap. However, they have limited resources, particularly memory. When refreshing the package repositories, it’s quite easy to encounter an Out-of-Memory (OOM) situation which results in the command - yum or dnf - is ‘killed’. For example:

$ sudo dnf update 
CentOS Stream 8 - AppStream                                                                                                  8.3 MB/s |  18 MB     00:02    
CentOS Stream 8 - BaseOS                                                                                                      13 MB/s |  16 MB     00:01    
CentOS Stream 8 - Extras                                                                                                      69 kB/s |  16 kB     00:00    
Google Compute Engine                                                                                                         20 kB/s | 9.4 kB     00:00    
Google Cloud SDK                                                                                                              24 MB/s |  43 MB     00:01    
Killed

dmesg has a lot of information about the situation, but the key line to confirm dnf caused the OOM event, is:

Read More
Graphify + MemMachine: 79× Token Reduction, Zero Vector Database

Graphify + MemMachine: 79× Token Reduction, Zero Vector Database

I help maintain MemMachine — an open-source long-term memory layer for AI agents. It’s a real codebase: 442 source files, 171 docs, a graph database, a SQL store, an MCP server, a REST API, a Python SDK, and integrations with eight different agent frameworks. When a new contributor asks “where does episodic memory actually get written?”, grep, the tool of choice for many AI coding assistants, doesn’t cut it. The answer threads through five files in three folders, plus a docker-compose service definition and a Helm chart. Each question you ask, it has to search all of these files, using the LLM to semantically understand the question and the files, then piece together an answer. This can take a lot of tokens and consume much of the context window.

Read More
How To Install and Boot VMWare VSphere/ESXi from Persistent Memory (or not)

How To Install and Boot VMWare VSphere/ESXi from Persistent Memory (or not)

In a previous post I described how to install and boot Linux using only Persistent Memory, no SSDs are required. For this follow on post, I attempted to install VMWare VSphere/ESXi v7.0u2 onto the persistent memory.

TL;DR - It doesn’t work. The installer doesn’t list the PMem devices, and I was unable to find a way to manually select the PMem device(s).

I assume you followed the previous post to configure sector namespaces that we’ll use to install ESXi.

Read More