Virtual Memory
Virtual Memory
Virtual Memory
6.7 Thrashing
Thrashing is a degenerate case that occurs when there is insufficient memory at one level
in the memory hierarchy to properly contain the working set required by the upper levels
of the memory hierarchy. This can result in the overall performance of the system
dropping to the speed of a lower level in the memory hierarchy. Therefore, thrashing can
quickly reduce the performance of the system to the speed of main memory or, worse yet,
the speed of the disk drive.
There are two primary causes of thrashing: (1) insufficient memory at a given level in the
memory hierarchy, and (2) the program does not exhibit locality of reference. If there is
insufficient memory to hold a working set of pages or cache lines, then the memory
system is constantly replacing one block (cache line or page) with another. As a result,
the system winds up operating at the speed of the slower memory in the hierarchy. A
common example occurs with virtual memory. A user may have several applications
running at the same time and the sum total of these programs' working sets is greater than
all of physical memory available to the program. As a result, as the operating system
switches between the applications it has to copy each application's data to and from disk
and it may also have to copy the code from disk to memory. Since a context switch
between programs is often much faster than retrieving data from the disk, this slows the
programs down by a tremendous factor since thrashing slows the context switch down to
the speed of swapping the applications to and from disk.
If the program does not exhibit locality of reference and the lower memory subsystems
are not fully associative, then thrashing can occur even if there is free memory at the
current level in the memory hierarchy. For example, suppose an eight kilobyte L1
caching system uses a direct-mapped cache with 16-byte cache lines (i.e., 512 cache
lines). If a program references data objects 8K apart on each access then the system will
have to replace the same line in the cache over and over again with each access. This
occurs even though the other 511 cache lines are currently unused.
If insufficient memory is the cause of thrashing, an easy solution is to add more memory
(if possible, it is rather hard to add more L1 cache when the cache is on the same chip as
the processor). Another alternative is to run fewer processes concurrently or modify the
program so that it references less memory over a given time period. If lack of locality of
reference is causing the problem, then you should restructure your program and its data
structures to make references local to one another.
1
Actually, virtual memory is really only supported by the 80386 and later processors.
We'll ignore this issue here since most people have an 80386 or later processor.
2
Strictly speaking, you actually get a 36-bit address space on Pentium Pro and later
processors, but Windows and Linux limits you to 32-bits so we'll use that limitation here.