CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 3 of 6 · 3 min read

One process

Think of examining one car from the outside, the dashboard, under the bonnet, the boot and the service book. Each view shows a different part, and they agree. The analogy stops in one place: a car has one dashboard, and a process has as many views as the kernel exposes.

In plain words: The worker program below is made so that its numbers are known. It asks for 100 MiB with malloc, writes to only 30 MiB (Level 7: pages are given on first touch), opens two files and starts two threads that sleep.

#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void *idle(void *arg) { (void)arg; sleep(60); return NULL; }

int main(void) {
    char *big = malloc(100u << 20);          /* ask for 100 MiB */
    memset(big, 1, 30u << 20);               /* touch only 30 MiB */
    open("/etc/hostname", O_RDONLY);
    open("/etc/passwd", O_RDONLY);
    pthread_t t[2];
    for (int i = 0; i < 2; i++) pthread_create(&t[i], NULL, idle, NULL);
    printf("worker pid %d ready\n", (int)getpid());
    fflush(stdout);
    sleep(60);
    return 0;
}

The five views, run while it sleeps:

$ ps -o pid,nlwp,vsz,rss,stat,comm -p PID
  PID NLWP    VSZ   RSS STAT COMMAND
  789    3 121504 32588 Sl   worker

$ ls -l /proc/PID/fd       (the names)
0 -> /dev/null
1 -> /tmp/l8/l12/w.out
2 -> /tmp/l8/l12/w.err
3 -> /etc/hostname
4 -> /etc/passwd

$ grep -E "^(VmSize|VmRSS|Threads|State)" /proc/PID/status
State:  S (sleeping)
VmSize:   121504 kB
VmRSS:     32588 kB
Threads:    3

$ pmap -x PID           (the total line and the biggest mapping)
total kB          121508   32588   30872
00007f22c47ff000  102404   30724   30724 rw---   [ anon ]

A worked example. Read the numbers against what the program did. NLWP is 3: the main thread and the two helpers. VSZ (virtual size) is 121,504 kB, which is about 118.7 MiB: the 100 MiB request is 102,400 kB, and the rest is the program, the C library and the thread stacks. RSS (resident set size) is 32,588 kB, about 31.8 MiB, so only the touched memory is real: 30 MiB is 30,720 kB, and the rest comes from the program code and libraries. The pmap line for the big anonymous mapping says 102,404 kB mapped and 30,724 kB resident, which is the same 30 MiB plus one page of bookkeeping. The state Sl means sleeping (S) and multi-threaded (l). The file descriptors 3 and 4 are the two files the program opened, and 0 to 2 are standard input, output and error.

The key lines. The gap between VSZ and RSS is the idea of Levels 6 and 7: a program can map far more memory than it uses, and only the touched pages cost real memory. An engineer who reads VSZ as "memory used" is misled.

Resident set size

The resident set size (RSS) of a process is the amount of its memory that is in physical memory now. It counts touched pages of the heap, stacks, program code and shared libraries, and not the pages that were mapped and never used or were swapped out.

Example: the worker maps 100 MiB, touches 30 MiB, and has an RSS of about 32 MiB.

Common mistake

Adding the RSS of all processes to find the memory in use. Shared libraries are counted in every process that maps them, so the sum is too large. Use free for the total, and pmap -x or the Pss field in smaps for a fair share.

Check yourself: 1. What does NLWP count? 2. Why is VSZ far larger than RSS for the worker? 3. Which descriptor numbers are standard input, output and error?