Per-process memory usage

There are several metrics to measure the amount of memory a process is using. I will begin with the two that are easiest to obtain— the virtual set size (vss) and the resident memory size (rss), both of which are available in most implementations of the ps and top commands:

The versions of top and ps from BusyBox give very limited information. The examples that follow use the full version from the procps pacakge.

The ps command shows Vss (VSZ) and Rss (RSS) with the options, -Aly, and a custom format which includes vsz and rss, as shown here:

Likewise, top shows a summary of the free memory and memory usage per process:

These simple commands give you a feel of the memory usage and give the first indication that you have a memory leak when you see that the Rss of a process keeps on increasing. However, they are not very accurate in the absolute measurements of memory usage.

In 2009, Matt Mackall began looking at the problem of accounting for shared pages in process memory measurement and added two new metrics called the unique set size or Uss, and the proportional set size or Pss:

The information is available in /proc/<PID>/smaps, which contains additional information for each of the mappings shown in /proc/<PID>/maps. Here is one section from such a file which provides information about the mapping for the libc code segment:

There is a tool named smem that collates the information from the smaps files and presents it in various ways, including as pie or bar charts. The project page for smem is https://www.selenic.com/smem. It is available as a package in most desktop distributions. However, since it is written in Python, installing it on an embedded target requires a Python environment, which may be too much trouble for just one tool. To help with this, there is a small program named smemcap that captures the state from /proc on the target and saves it to a TAR file which can be analyzed later on the host computer. It is part of BusyBox, but it can also be compiled from the smem source.

Running smem natively, as root, you will see these results:

You can see from the last line of the output that, in this case, the total Pss is about a half of the Rss.

If you don't have or don't want to install Python on your target, you can capture the state using smemcap, again as root:

Then, copy the TAR file to the host and read it using smem -S, though this time there is no need to run as root:

The output is identical to that when running it natively.

Another way to display Pss is via ps_mem (https://github.com/pixelb/ps_mem), which prints much the same information but in a simpler format. It is also written in Python.

Android also has a tool named procrank, which can be cross compiled for embedded Linux with a few small changes. You can get the code from https://github.com/csimmonds/procrank_linux.