Matt Mackall has recently released at the Embedded Linux Conference, a set of patches that improves the way memory usage information is made available.
The memory usage process on Linux is complicated and it deals with many direct or indirect factors. The most commonly used tool to get a memory usage report is the
ps tool, which outputs information about processes, such as the process id (PID), the current running state(S), VSZ (virtual set size) and RSS (resident set size). But the information reported by
ps does not exactly represent the real current memory usage of a process, but more like an estimation of how much memory a process would use if it is the only one running.
More detailed questions, such as which are the parts taking up most memory, are even harder to be answered. Matt Mackall's project is intended to fix this up and provide some more usable information on the memory usage report.
The way Matt's patches work is quite simple, and logical. First of all, there is a file called
pagemap added to each process' directory that contains the page frame number for each page in the process's address space. This file provides even comparisons between processes to see the shared pages.
The
/proc/kpagemap file is used for providing information on the kernel's memory map, as kpagemap contains the mapping count and page flags for each physical page. Matt's patchset introduces two new metrics, the PSS (proportional set size) and the USS (unique set size) representing a count of processes that currently are stored in the memory, respectively the number of pages to be returned to the system in case the process is killed.
In order to calculate these numbers, which are very difficult to be computed as they require a pass through the process' address space, there are a couple of tools to do it well; as, for example, the
memstats tool or the
memrank tool that list the processes by decreasing PSS.
You can download these patches from
here.