Hack #34. Manage the Paging File

Windows can get confused about what memory is and how it uses it. Giving Windows some guidance will keep things running smoothly.

Ever encounter an "out of memory error"? Been advised by an on-screen dialog message to "close some applications to free up memory"? These errors are usually not about adding or removing memory, but about how the operating system uses the memory available. Windows and some applications can present any number of "out of memory" error messages, such as:

  • "There is not enough memory available to run this program. Quit one or more programs, and then try again."

  • "Insufficient memory to initialize windows. Quit one or more memory-resident programs or remove unnecessary utilities from your Config.sys and Autoexec.bat files, and restart your computer."

  • "The system is dangerously low on resources. Would you like to terminate the following application..."

Microsoft Windows 3.x-95/98/Me manage many types of memory—mostly RAM, but within and beyond the obvious system RAM, other resources that store data while the operating system and applications do their things.

There are three causes for "out of memory" conditions under Windows 9.x: truly having too little RAM to support the operating system, having too little free disk space for the operating system to be able to swap memory contents to the swapfile on a hard drive, or forcing the swapfile size into too small and overcrowded small segments of memory called "system resources."

A solution to the first problem is of course to add more memory [Hack#32] . When you've reached that point, 64-128MB of RAM or more if applications need it, it's time to look elsewhere for what's keeping Windows from being happy [Hack #35] or examine the Windows swapfile

The paging file or swapfile is controlled by Windows. While users can configure how much disk space to allocate to this file, only Windows decides how and when it is used. Left on its own, Windows could create a swapfile that is larger than necessary, or you could manually configure an unreasonably large swapfile yourself and waste hundreds of megabytes of disk space.

If the hard drive has too little free space available for making a swapfile, Windows can indicate "out of memory," meaning it could not swap out enough RAM contents to disk to leave your applications or data and the operating system enough memory to work with.

If the swapfile size is left for Windows to decide, it will also suffer fragmentation and be broken up across many different regions of the disk drive, further reducing performance. Swapfile fragmentation will occur if there is no single area of free space large enough to contain the entire swapfile, or, if the swapfile starts out at one size, files are subsequently stored "around it," and then the swapfile has to grow into a separate area of free space.

Tip

Defragmenting your hard drive every week, certainly every month, is an excellent way to improve both filesystem and swapfile performance. But see "Hacking the Hack" for instructions specific to Windows NT and later.

There are several rules of thumb that can be applied to configuring the size of the swapfile. In most cases, the swapfile size should be at least equal to the amount of RAM installed on the system—that is, if you have 32 MB of RAM, the swapfile should equal at least 32 MB of disk space and preferably 2-3 times more (64-96 MB). The debate rages at this point whether or not the size of the swapfile should be exactly equal to, 150%, or 200%, or more of the amount of RAM in the system. Depending on the amount of RAM installed, most of us use 150% as the maximum value to give Windows a little extra room, but not an excessive amount, to play with.

It is important to remember that each version of Windows has a minimum RAM requirement, as shown in Table 4-1. Your system should have enough RAM to accommodate Windows (the minimum requirement) plus your applications and data if you want to reduce swapfile use and manage its size.

Setting the swapfile size tells Windows how much disk space it can play with when it decides it's time to swap a program or data out the file. You want Windows using the hard drive sparingly, and when it does use it, you want it to use it as efficiently as possible. So how do you determine what "size" to make the swapfile?

Tip

All swapfile setting recommendations come with an explicit or implied "rule of thumb" caveat. Most of the recommendations exist for two reasons: a desire or need to limit the amount of disk space the swapfile consumes and to try to contain or limit the fragmentation of the swapfile and other files on the same drive.

There are as many people telling us not to set a specific swapfile size as there are telling us what values to use for setting the swapfile. Nowhere is it documented that manually setting the swapfile size does any harm, unless you try to set it too low.

Windows will allow you to configure no swapfile but will "complain" at startup with a strong message telling you to create a swapfile. As a built-in "self-preservation" feature, even with a zero-byte or no swapfile setting, Windows will create a swapfile anyway.

If you have less than 128 MB of RAM, set the swapfile to be at least equal to, or up to 150% of, the amount of RAM. At less than 128 MB of RAM, Windows is going to swap out idle programs and data more than it would with 128 MB of RAM. Going to a 150% swapfile size gives Windows room to rattle around and enough space for applications to play in memory, and enough swapfile space to swap it all out if necessary.

If you have more than 128 MB of RAM, you have to start believing that you have more than enough RAM to keep the operating system (at least Windows 95-Me) and most of your applications happy, since you've met the minimum RAM requirement for the operating system plus additional RAM for applications. At 128, 256, or 512 MB of RAM, the swapfile size should be equal to no more than the amount of RAM—after all, even a portion of 128, 256, or 512 MB is a lot of disk space to waste and a lot of data to read and write on a disk drive if Windows has to swap out, which considerably slows the system performance but is better than running out of memory.

Tip

Windows XP is very "smart" about memory use and the swapfile. If you set the swapfile size too small and then run multiple large applications and open multiple documents, Windows XP can take precedence, ignore your maximum swapfile size setting, and force a larger swapfile size to protect itself and your work when both RAM and swapfile use have "maxed out."

It is obviously far better to add more RAM than hit the ceiling of RAM+swapfile size or keep increasing the swapfile size to overcome having too little RAM. For systems with abundant amounts of RAM, certainly at the 512 MB to 1 GB level, setting the swapfile to no less than 50% of the amount of RAM typically provides good performance. However, if you frequently use multiple large applications or have multiple large datafiles open, the swapfile should equal the amount of RAM so Windows has adequate space to swap to. Table 4-3 gives recommendations for swapfile sizes.

Table 4-3. Recommended swapfile sizes based on amount of RAM

Total system RAM

Recommended swapfile size

16 MB

32 MB

32 MB

32-64 MB

64 MB

64-96 MB

128 MB

128 MB

256 MB

256 MB

512 MB

256-512 MB

768 MB

512-768 MB

1,024 MB (1 GB)

512-1,024 MB

To ensure the best performance of the swapfile and prevent the swapfile from changing size and becoming fragmented, set the minimum and maximum size of the file to the same values. For a recently defragmented PC, setting the swapfile size will create a new, unfragmented swapfile. If you set the minimum and maximum sizes to different values, the swapfile can become fragmented and only a disk defragmenter that can defragment the swapfile can put it back together for a short time.

To set the swapfile size, follow these specific instructions for your operating system:

  1. Right-click the My Computer icon.

  2. Select Properties and then follow the instructions below for your particular operating system.

Windows 95, 98, 98SE, Me

  1. Select the Performance tab, then the Virtual Memory button.

  2. The default setting is "Let Windows manage my virtual memory settings." Instead, select "Let me specify my own virtual memory settings."

  3. Type in the size you want in both the Minimum and the Maximum edit boxes.

  4. Click OK to close the dialogs but do not restart the system yet; there is one more change to make in the system configuration.

  5. See [Hack #36] for the next set of steps to follow before restarting your computer.

Windows NT, 2000, XP, 2003

  1. Select the Advanced tab.

  2. In the Performance section, click on the Settings button. The Performance Options dialog appears.

  3. Under the Virtual memory section, click the Change button. The Virtual Memory dialog appears.

  4. Select the "Custom size:" radio button.

  5. Type in the minimum and maximum amount of disk space you want to allocate for the swapfile size.

  6. Click OK to save the information and close the dialogs, but before you restart your PC, take a look at [Hack #37 .

Hacking the Hack

Windows NT, 2000, XP, and Windows Server 2003 can't defragment the pagefile, because it is open for exclusive use while the system is up and running. Sysinternals PageDefrag (http://www.sysinternals.com/ntw2k/freeware/pagedefrag.shtml) gets around this problem by defragmenting your pagefile at the next reboot: it uses the same defragmentation mechanism that Windows uses, but it runs before the operating system has opened the pagefile for exclusive use.

Get PC Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.