[ Team LiB ] Previous Section Next Section

Virtual Memory

Even before anyone conceived the idea of the first personal computer, mainframe computer designers faced the same tradeoff between memory and mass storage. Mass storage was plentiful and cheap; memory was expensive, so much so that not even large corporations could afford as much as they wanted. In the early years of computers, engineers tried to sidestep the high cost of memory by faking it, making computers think they had more memory than they actually did. With some fancy footwork and a lot of shuffling around of bytes, they substituted mass storage for memory. The engineers called the memory the computer thought it had but in reality didn't exist virtual memory. Although the cost of memory has fallen by a millionfold, memory use has risen even faster. Programs' need for memory still outpaces what any reasonable person or organization can afford (or wants to pay for), so virtual memory not only exists but is flourishing.

Microprocessors cannot ordinarily use disk storage to hold the data they work on. Even if they could, it would severely degrade the performance of the system because the access time for disk storage is thousands of times longer than for solid-state memory. To prevent performance problems and keep active programs in solid-state memory where your computer's microprocessor can use them, virtual memory systems swap blocks of code and data between solid-state and disk storage.

Demand Paging

Most modern computers take advantage of a feature called demand paging that has been part of all Intel microprocessors since the 386. These chips are able to track memory content as it is moved between disk and solid state memory in 4K blocks. The microprocessor assigns an address to the data in the block, and the address stays constant regardless of where the data actually gets stored.

Once solid-state memory reaches its capacity, the virtual memory system copies the contents of one or more pages of memory to disk as the memory space is required. When the system needs data that has been copied to disk, it copies the least recently used pages to disk and refills their space with the disk-based pages it needs. When your system attempts to read from a page with an address that's not available in solid-state memory, it creates a page fault. The fault causes a virtual memory manager routine to handle the details of exchanging data between solid-state and disk storage.

This reactive kind of control is called demand paging because it swaps data only when the microprocessor demands unavailable addresses. It makes no attempt to anticipate the needs of the microprocessor.

Virtual memory technology allows your computer to run more programs than would be otherwise possible given the amount of solid-state memory in your system. The effective memory of your system approaches the spare capacity of your disk. The downside is that it takes away from the capacity of your hard disk (although disk storage is substantially less expensive than solid-state memory by about two orders of magnitude). In addition, performance slows substantially when your system must swap memory.

Virtual memory is an old technology, harking back to the days of mainframe computers when, as now, disk storage was cheaper than physical memory. Many DOS applications took advantage of the technology, and it has been part of every version of Windows since Windows 386—which means not only before Windows 95 but also Windows 3.1.

Windows uses a demand-paging system that's based on a least-recently used algorithm. That is, the Windows virtual memory manager (VMM) decides which data to swap to disk based on when it was last used by your system. The Windows VMM also maintains the virtual memory page table, which serves as a key to which pages are used by each application, which are kept in solid-state storage, and which are on disk.

Windows decides which pages to swap to disk using two flags for each page. The accessed flag indicates that the page has been read from or written to since the time it was loaded into memory. The dirty flag indicates the page has been written to.

When Windows needs more pages in solid-state memory, it scans through its page table looking for pages showing neither the accessed flag nor the dirty flag. As it makes its scan, it resets the accessed flag but not the dirty flag. If it does not find sufficient unflagged pages, it scans through the page table again. This time, more pages should be unflagged because of the previous resetting of the accessed flags. If it still cannot find enough available pages, the Windows virtual memory manager then swaps pages regardless of the flags.

Swap Files

The disk space used by a virtual memory system usually takes the form of an ordinary file, though one reserved for its special purpose. Engineers call the virtual memory file a swap file because the memory system swaps data to and from the file as the operating system requires it. Although earlier versions of Windows had several options you could choose for swap files—primarily the choice between a temporary file that swelled as you used it and a permanent file that forever stole disk space—Windows 95 erased such complications.

Under Windows 95 as well as Windows 98, the swap file mixes together features of temporary and permanent swap files. Like a temporary swap file, the current Windows swap file is dynamic, expanding as your system demands virtual memory and contracting when it does not. In addition, it can shuffle itself into the scatter clusters of a fragmented hard disk. It can even run on a compressed hard disk.

Windows gives you full menu control of its swap file. You can start the virtual memory control system by clicking the Virtual Memory button in the Performance tab of Device Manager. (You start the Device Manager by clicking the System icon in Control Panel.) Once you click the Virtual Memory button, you should see a dialog box like the one shown in Figure 15.2.

Figure 15.2. The Virtual Memory dialog box for controlling Windows 95 (and Windows 98) swap files.


You can opt to let Windows choose the size and place of your swap file or take direct control. By default, Windows puts your swap file in the Windows directory of your C: drive. By selecting the appropriate box, you can tell Windows the disk and directory in which to put your swap file and set minimum and maximum limits for its size.

    [ Team LiB ] Previous Section Next Section