Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


Defragmenting Your Pagefile

by Mitch Tulloch, author of Windows Server Hacks
11/23/2004

Defragmenting your hard drive regularly is an important part of general system maintenance for Windows XP machines, and the Disk Defragmenter tool in Computer Management lets you do this easily. (You can also get to the Disk Defragmenter tool by choosing Control Panel-->Performance and Maintenance-->Rearrange items on your hard disk to make programs run faster.) But what about defragmenting your pagefile?

Windows dynamically resizes your pagefile as needed to ensure there is sufficient virtual memory to meet the needs of running applications, and as a result of this resizing, it is possible that your pagefile can become fragmented over time, which can slow performance of your machine. Unfortunately, the Disk Defragmenter tool doesn't defragment the pagefile, and you might be tempted to buy a third-party disk defragmenter to do this. However, you can save your bucks by following this simple procedure to defragment your pagefile.

First, here's a look at the fragmentation of the C: drive on my XP machine just before I defragmented it:

Figure 1
Figure 1

After running Disk Defragmenter, the fragmentation of this drive is much reduced:

Figure 2
Figure 2

Let's look at the report generated by Disk Defragmenter to see the condition of the pagefile:


Volume (C:)
    Volume size                                = 4.00 GB
    Cluster size                               = 4 KB
    Used space                                 = 2.59 GB
    Free space                                 = 1.41 GB
    Percent free space                         = 35 %

Volume fragmentation
    Total fragmentation                        = 0 %
    File fragmentation                         = 0 %
    Free space fragmentation                   = 1 %

File fragmentation
    Total files                                = 12,927
    Average file size                          = 259 KB
    Total fragmented files                     = 0
    Total excess fragments                     = 0
    Average fragments per file                 = 1.00

Pagefile fragmentation
    Pagefile size                              = 767 MB
    Total fragments                            = 3

Folder fragmentation
    Total folders                              = 863
    Fragmented folders                         = 1
    Excess folder fragments                    = 0

Master File Table (MFT) fragmentation
    Total MFT size                             = 14 MB
    MFT record count                           = 13,815
    Percent MFT in use                         = 99 %
    Total MFT fragments                        = 4

Note the small degree of pagefile fragmentation (3 fragments). Because of the way Windows XP manages the pagefile swapping large blocks of data, the pagefile usually doesn't become very fragmented, even on heavily used machines. It can become defragmented enough, though, to affect system performance.

To defragment the pagefile, we first have to remove the pagefile, then defragment the machine, and finally recreate the pagefile. To do this, start by opening the System utility in Control Panel. (You may have to get there by clicking Performance and Maintenance from the Control Panel and then clicking System.) Then select the Advanced tab, click the Settings button under Performance, select the Advanced tab of Performance Options, and click Change to view your Virtual Memory settings:

Figure 3
Figure 3

Now select the No Paging File option and click Set. This will disable paging entirely and should also remove the pagefile (pagefile.sys) from the hard drive the next time you reboot your system. (If it doesn't do this, you can delete this file manually after rebooting. It's normally found in C:\.)

Reboot your machine, and when Windows comes up, use Disk Defragmenter to defragment the volume where your pagefile was previously (C: drive in my example). Once your system is defragmented, open the Virtual Memory box again, and change the pagefile back to System Managed Size. Then, reboot your machine again. This time, when you run Disk Defragmenter, it should say your pagefile has only one fragment. When I ran Disk Defragmenter on my machine after performing these steps, the result was this:

Figure 4
Figure 4

Note that the pagefile (green area) has been moved to the end of the volume (an added bonus to reduce future file fragmentation). Also, as expected, the report generated by Disk Defragmenter indicates there is now no fragmentation of the pagefile:


Volume (C:)
    Volume size                                = 4.00 GB
    Cluster size                               = 4 KB
    Used space                                 = 2.59 GB
    Free space                                 = 1.41 GB
    Percent free space                         = 35 %

Volume fragmentation
    Total fragmentation                        = 0 %
    File fragmentation                         = 0 %
    Free space fragmentation                   = 0 %

File fragmentation
    Total files                                = 13,194
    Average file size                          = 254 KB
    Total fragmented files                     = 10
    Total excess fragments                     = 32
    Average fragments per file                 = 1.00

Pagefile fragmentation
    Pagefile size                              = 767 MB
    Total fragments                            = 1

Folder fragmentation
    Total folders                              = 865
    Fragmented folders                         = 1
    Excess folder fragments                    = 0

Master File Table (MFT) fragmentation
    Total MFT size                             = 14 MB
    MFT record count                           = 14,084
    Percent MFT in use                         = 99 %
    Total MFT fragments                        

For more tips on optimizing use of your pagefile, see my previous article Optimizing Your Servers' Pagefile Performance on WindowsDevCenter.

Mitch Tulloch is the author of Windows 2000 Administration in a Nutshell, Windows Server 2003 in a Nutshell, and Windows Server Hacks.


Return to the Windows DevCenter.

Copyright © 2009 O'Reilly Media, Inc.