Lecture 16: Memory leftovers


Thrashing occurs when processes are actively using more memory than is physically present. This causes a state of continuous paging; processes run for a short time, immediately try to page in some data, causing another process to run, which itself pages in data, and so forth.

Thrashing can be easily addressed: when the system starts thrashing, choose a process or set of processes, and either kill or suspend them (suspension may be sufficient if processes only need lots of memory for a short time; they can be finished one by one). A good choice for termination would be the process that is using the most memory.

The only difficulty is in estimating the amount of memory that is currently being used. Because processes share physical memory using a heuristic replacement algorithm instead of acquiring and releasing it, the total amount "in use" is a bit of a fuzzy concept.

A good approximation is to track the working set of each process: the set of pages that have been accessed with in the past n time units. The size of the working set gives a rough idea of how much memory the process is actively using.

By tracking working sets, we can detect thrashing by comparing the total working set size to the number of frames of available memory. We can also make use of the size of the working sets when determining which processes to suspend.

Context switching

Recall that each process has its own page table. The address of the page table is called the page table base register (PTBR). When we switch to a new process, we must update the PTBR to refer to the new process's page table (which is stored in the PCB).

Because there is a new active page table, all entries in the TLB are no longer valid. Therefore the TLB must be flushed. As the new process runs, it will generate a large number of TLB misses until the pages it is actively using have been entered in the TLB. Handling TLB faults is expensive; this is one of the biggest overheads involved in context switching.

To reduce this overhead, many TLBs allow entries from multiple processes to be stored: instead of flushing when context switching, we simply change the active processes number, which is used along with the page number to index into the TLB.

The extra identifiers are often called tags; TLBs that have them are called tagged TLBs.