Sunday, January 3, 2010

Optimizing Windows Paging/Swap File

Optimize Windows 10, 7, Vista and XP's Swap file by following these steps, which results in a slightly faster computer with less disk fragmentation. This is an admittedly geeky thing to do. Article updated for Windows 10.

Contents:
  • Why optimize the Paging File
  • Set Paging File Size to Zero
  • Defragment the Disk
  • Re-allocate Paging File to Fixed Size
  • Recommended Sizes

What is a Swap File?

The "Swap file", also known as a "Paging File" or "Virtual Memory" is used by Windows to extend the PC's memory by simulating (virtualizing) memory on the hard disk. It works like this: As more programs and applications are loaded into memory, you may exceed the amount of physical RAM. When this happens, Windows takes the least-used program and "pages-it" to the hard disk. The paged-out program doesn't know it was moved into Virtual memory and while there, it is in suspended animation.

When you switch back to the application, Windows pulls it from the hard disk's paging file and crams it back into memory, while shoving something else to disk. Paging only happens when you run out of memory and there are too many programs are running at the same time. Although paging is a wonderful idea, it is slow. PCs that constantly dip into the paging file really need more RAM.


By Default, Paging Files Fragment

When Windows is first installed, a paging file is automatically defined, with a minimum and maximum size. With each reboot, Windows re-allocates (re-creates) the file, starting with the minimum size. This can be large -- often 1.5 times physical RAM. In other words, if your PC has 2G of RAM, then the paging file is likely about 2.0 to 3G. With each reboot, the old paging file is destroyed and a new one is constructed

As Windows allocates the new file, it starts writing on the first available disk cluster, but because the file is so large, it invariably runs out of space and the file is fragmented into multiple locations across the hard disk. For most systems, the paging file fragments into hundreds, often thousands of different locations. As the computer dips into virtual memory, the file continues to expand and fragment, up to its maximum size. This causes other files to fragment more often than normal.

Although it is not a true representation of the hard disk, Windows XP Defragmentation screen hints at the Swap file's location. In the illustration, the green, unmovable clusters are likely the swap file. The file fragments across other clusters too small to see in the graphic.  I wish this detail were still available in the new operating systems:




Optimizing the Paging File:

With the steps outlined here, you can force a permanent, fixed-location swap file and you can keep it from allocating and de-allocating. This helps keep other files from fragmenting as much and you will see slightly faster boot times.

Update:  Added Windows 10 steps; see below for Windows 7, along with more details.  The steps are nearly identical.

Windows 10 Instructions:
Illustrated details below, in the Windows 7 section.  Steps are nearly identical.

a)  Launch Windows Explorer
b)  Highlight "This PC / My PC", "other-mouse-click", choosing "Properties"
c)  Select Advanced System Settings
d)  On top-tabs, select Advanced.  Click "Performance"
e)  Click the second "Advanced" tab
f)  Click "Virtual Paging"

Uncheck [ ] Automatic manage paging file size
Set "No paging file"  (Set)
Click OK, OK.  Ignore warnings.
Reboot to de-allocate the file.

g)  After reboot, manually defragment the hard disk:

-  Open File Explorer
-  Highlight drive C:, other-mouse-click, "Properties"
-  In the Tools Tab, click Optimize and defragment drive.
-  Allow defragment to complete.

h)  Return to the Virtual Paging screens, above

i)  Manually set a Virtual Paging Size.  The numbers are not important but for most machines, I pick 1500-1500, or 1800-1800, where both the min/initial and max sizes are the same. Click "Set" to commit.


This fixes the swap file on the disk, making it immovable and permanent.  This file is the number-one cause of fragmentation on your disk.

Windows 7 Instructions:

It is easy to optimize the Swap File but it takes time and a few reboots. Follow these steps, in this order:

Delete the existing Swap file - Set to Zero

1. Open System Properties:

Windows 7
  • Other-mouse-click (right-mouse) "Computer" (e.g. MyComputer)
  • Choose "Properties"
  • "Advanced System Settings"
2. In System Properties, click the [Advanced] tab.
  • Click Performance's "Settings" button

(double-click image for larger view; click "right-x" to return)

3. On the Performance Options screen,
  • Once again click the [Advanced] tab
  • Click the Virtual Memory "Change..." button


4. Inside of the Virtual Memory dialog:
  • [ ] Uncheck "Automatically Manage"
  • Set "No Paging File"

  • Ignore the warning message
  • Click OK and close all dialogs
  • Reboot (you must reboot now)


Defragment the Disk

Once rebooted, defragment the disk. This forces the soon-to-be created new swap file to arrive in a contiguous disk space with minimal fragmentation. Although this step takes time, it involves only a few clicks:

5. In Windows Explorer:
  • "Other-mouse-click" the C: drive .
  • Choose "Properties", then [Tools].
  • Click "Defragment Now".
You only need to defragment the C: drive.
Wait for the defragmentation to complete.

Note: Windows 7 displays a progress-indicator but Vista gives no indication when it will complete. Defragging can easily take an hour or more. With Vista, wait until the "Cancel defragmentation" button changes back to "defragment now" -- then you will know you are done.

Re-Allocate the Swap File

When the disk is defragmented, return to the Virtual Memory dialog box (see above) and re-build the paging file, following these steps.

6. In the Virtual Memory dialog:
  • Set a Custom Size, where both the minimum and maximum sizes are the same.

Recommended Swapfile Size:

Recommended sizes are about 1.5 times your physical memory.
But for reasons explained in a moment, do not exceed 2G (2048) or at most 3G of disk, regardless of how much RAM you have.

For example:
2GB to 4GB RAM, set to 1200, 1200
8GB RAM, set to 1500,1500
16GB RAM, set to 1800,1800

These are not hard-set rules, just recommendations.  If you ever ran out of space, re-do these steps and pick a larger size.  I have never met anyone who has had to do this.


The key to setting the size is to set both the minimum and maximum to the same value --This keeps the file from growing and shrinking (and thus fragmenting).

The actual number chosen is not that important and it does not need to be in the increments above. Once set, ignore any warnings Windows might display, and reboot the computer one last time.


Following the 1.5x Rule:

Contrary to most publications, even if you have a two or more gigabytes of RAM, do not bother setting the swap file much larger than 1.5 to 2G. Larger swap files take even longer to parse and the performance-loss may not be worth the trouble.

Keep in mind, the swap file only has to be large enough to hold the largest swapped-out program and its data -- it does not need to hold the entire memory pool. Besides, at some point, it is useful to actually run out of Swap File space; when you do, Windows displays "out of virtual memory." This is your hint to spend $70 and buy more RAM; when you do, your computer will run faster.

What if you set the swap file to a larger size? No harm done - except you will have a huge file on the hard disk that likely adds no benefits to your system and it will slow down backup programs.

You are now done optimizing the Swap file.


Additional Notes:If you have a separate physical drive, there is some benefit in moving the swap file to a different physical drive, as long as that drive is different than the operating system's disk. Most of the benefit is because fragmentation is minimized, but if you follow the steps above, this is a moot point and for this reason, it is not worth the trouble.

If you do this, be sure the secondary drive is a "real" disk and not a second partition on the same physical drive. Contrary to many reports, placing the swap file on a second partition actually slows the computer down because the read-write head has to jump from partition-to-partition as files are paged. At least with a second physical disk, each read-write-head travels independently.

In any case, if you are dipping into the Swap file you really need more RAM, or at the very least, close some programs.

Updates:
A friend of mine pointed out a Microsoft Systernal program that can defragment the hard disk without going through the trouble to of setting the file to 0-bytes and defragging. On Microsoft's site, search for this phrase: "Systernal PageDefrag". Download and install the utility (I have not looked at this utility with Windows 7 or Vista). With this, you still have to reboot, but an existing page-file can be defragmented. Of course, this is still a moot point if you don't change the min-max size to the same number, as described above.

Related Articles:
Vista Spiffs - For performance
Taming Browser Cookies
Stopping the most annoying Vista UAC Nags
Streamlining the Start Menu
Windows Folder Locations

No comments:

Post a Comment

Comments are moderated and published upon review.