Virtual Memory & Demand Paging

the working set

Think of the small stack of tools a mechanic keeps on the tray beside the car for the job at hand — not the entire toolbox, just the wrenches and sockets this particular repair needs right now. As the job changes, the stack on the tray changes too, but at any moment it is small and specific. The working set of a process is that tray: the set of pages the process has actually used in the recent past, which is a good guess at the pages it needs to keep handy right now.

Concretely, the working set is defined over a window: pick a window size, say the most recent delta memory references, and the working set is the set of distinct pages touched within that window. As the program runs, this set slides forward in time, gaining pages the program newly touches and dropping pages it has stopped using. Its size — the working-set size — measures how many frames the process genuinely needs to run comfortably without constant faulting. Because of locality, the working set is usually much smaller than the program's whole address space and changes only gradually, except at sharp transitions (called locality changes) when the program switches to a new phase, such as moving from loading data to crunching it.

Why it matters: the working set is the bridge from locality to the practical question of how much memory to give a process, and from there to thrashing. The rule of thumb is simple and powerful: if every running process can keep its working set resident in RAM, faults stay rare and the system runs smoothly; but if the total of all processes' working sets exceeds available frames, some process is forced to run with fewer frames than its working set needs, faults explode, and the system thrashes — spending nearly all its time paging instead of computing. The OS can use this to decide the right degree of multiprogramming: admit processes only while their working sets fit. The honest caveat: the true working set needs the future, so real systems only approximate it, for instance by sampling reference bits over time.

Over the last 10,000 references a process touched pages {2, 3, 5, 9}. Its working set is those 4 pages, so it needs about 4 frames to run without thrashing. When it later starts a new phase touching {40, 41, 42}, the working set slides to the new pages.

Give a process at least its working-set size in frames, or it thrashes.

The working set is defined over a recent-past window, but its purpose is to predict the near future — which works only because of locality. The exact set cannot be known without seeing the future, so real OSes estimate it, and the window size delta must be chosen with care.

Also called
working-set model工作集合工作集模型