Download the code for this article: PageDefrag (70KB)
Your average Windows® defragmenter can optimize most files on your hard drive, but some items are beyond the powers of most defragging tools. Files that are open and locked for exclusive use, such as the Windows page file and Registry hives, aren't normally processed by the built-in Windows defragmenter or by third-party utilities. The standard Windows APIs don't support defragmentation of these files while they're in use. Luckily, the PageDefrag utility, created by Mark Russinovich for Sysinternals, saves the day by defragging items untouched by other tools.
PageDefrag can defragment the Windows page file, Registry hives, event logs, and the hibernation file (the area on disk where memory is saved when a notebook jumps into hibernation mode). The program optimizes these files during the PC's bootup sequence before they're in use.
To run PageDefrag, just double-click the pagedfrg.exe file. The program displays the name of each file it can process for defragmentation (see Figure 1). The page file is pagefile.sys. The Registry hives appear as individual files for each hive—DEFAULT, SAM, SECURITY, SOFTWARE, and SYSTEM. The event logs also display as their individual files; for example, AppEvent.evt is the Application log, SecEvent.evt is the Security log, and SysEvent.evt is the System log. Finally, the hibernation file is Hiberfil.sys.
Figure 1** PageDefrag displays the names of each file it can process **(Click the image for a larger view)
Next to each file name is a Clusters column, which shows you how many clusters on the disk are allocated to the file, and a Fragments column, which indicates how many fragmented, or noncontiguous, pieces of the file are scattered around the disk. Based on this data, you can determine whether or not the files need defragmentation.
You have two options: you can defrag the files at the next bootup or at every bootup. Unless the files are heavily used or the PC is a server or other box that's rarely rebooted, you should be fine just defragmenting them at the next bootup. You can also enable a countdown so you have time to cancel the defragmentation as soon as the PC starts up.
The next time you reboot the computer, the normal chkdsk command runs, and then PageDefrag goes into action. Files already organized into contiguous clusters are bypassed. Those broken into fragments are optimized one by one. The program displays the name of each item being defragmented, tells you if the defrag is successful, and lists the number of clusters allocated to the file, both before and after defragmentation.
PageDefrag may not be able to optimize a file if the PC has too little available disk space or if the free space itself is too fragmented. In these cases, you'll need to clear up more space and run a defragmenter within Windows to optimize the free space and then try PageDefrag again.
PageDefrag usually runs quickly as it's tackling only a small number of files. However, if the files are heavily fragmented, it may take some time to complete, at least the first time. After PageDefrag finishes its work, Windows loads normally. You can then open the program again to confirm that each file is now a single fragment.
You can run PageDefrag non-interactively via the command line or through a script by using the options -e Defrag boot, -o Defrag once, -n Never Defrag, and -t Set countdown to specified number of seconds with the pagedfrg.exe file.
To learn more and download a copy, visit download.sysinternals.com/files/DebugView.zip.
Lance Whitney is an IT consultant, trainer, and technical writer. He has spent countless hours tweaking Windows workstations and servers. Originally a journalist, he took a blind leap into the IT world 15 years ago.
© 2008 Microsoft Corporation and CMP Media, LLC. All rights reserved; reproduction in part or in whole without permission is prohibited.