Disk Fragmentation and System Performance

Published 14 March 08 06:00 AM

When addressing system performance issues, a key element that is often overlooked is Disk Fragmentation.  Even on a brand new system with plenty of RAM and high-end processors, the performance of the hard disk may be a bottleneck causing system performance issues.  It takes time to load large data files into memory - issues become particularly noticeable when dealing with movies, video clips, database files or .ISO image files which may easily be several gigabytes in size.  On a freshly formatted disk, these files load fairly quickly.  Over time, however you may start to notice performance degradation - caused by disk fragmentation.

We touched on disk fragmentation when we were discussing the Page File a couple of months ago, but we never really got into the nuts and bolts of it.  To understand disk fragmentation though, you need to understand the basic structure of hard disks.  When you format a hard disk, the formatting process divides the disk into sectors, each of which contains space for 512 bytes of data.  The file system then combines groups of sectors into clusters.  A cluster is the smallest unit of space available for holding a single file - or part of a file.  On NTFS disks, the cluster sizes are determined based on the drive size as shown below (this information is also available in Microsoft KB 314878).  When formatting disks it is possible to change the cluster size, however this may cause additional performance issues.

Drive Size (Logical Volume) Cluster Size Sectors
512MB or less 512 bytes 1
513MB - 1,024MB (1GB) 1,024 bytes (1kb) 2
1,025MB - 2,048MB (2GB) 2,048 bytes (2kb) 4
2,049MB + 4,096 bytes (4kb) 8

Using the information above, if you were to take a 100MB video file, the file would be divided into roughly 25,000 pieces.  If you save this 100MB file onto a freshly formatted disk, the information would be written in contiguous clusters.  Since all of the clusters holding the data for this file are physically adjacent to each other, the mechanical components of the hard disk work very efficiently, pulling the data in one operation.  In addition, the hard disk's cache and the Windows disk cache can anticipate data requests and fetch data from nearby clusters.  This data can then be retrieved by an application from cached memory which is faster than retrieving the information from the disk itself. 

Seems pretty straightforward, right?  The problem is that the hard disks don't stay neatly organized for very long.  Whenever you add data to an existing file, the file system has to allocate more clusters for storage.  Typically, these clusters wind up being in a different physical location on the disk.  As you delete files, you create gaps in the arrangement of the contiguously stored files.  As you save new files (and this is especially true for large files), the file system uses up all of these bits of free space - resulting in the new files being scattered all over the disk in noncontiguous pieces.  And thus we end up with fragmented disks and system performance issues because the disk heads have to spend time moving from cluster to cluster before they can read or write the data.

Enter Disk Defragmenter.  This utility physically rearranges the files so that they are stored (as much as possible) in physically contiguous clusters.  In addition to the consolidation of files and folders, the Defragmenter utility also consolidates free space - meaning that it is less likely for new files to be fragmented when you save them.  For operating systems prior to Windows Vista, you had to manually run the utility or schedule automatic defragmentation via a scheduled task.  On Windows Vista, Disk Defragmenter runs as a low-priority background task that is automatically run on a weekly basis without requiring user intervention.  On Windows Server 2008, which uses the same Disk Defragmenter, the automatic defragmentation is not enabled by default.  Also, the color-coded display that was part of earlier versions of the utility has been retired (believe it or not, more than a few people have asked about that!).  Aside from the GUI version of the tool, you can also use a command-line version that enables some more granular control over the process.  The utility name is DEFRAG.EXE and does require administrative privileges to run.  The basic operation of the utility involves passing it a driver letter, for example: defrag.exe c: would perform a defragmentation of the C: drive.  You can also specify other options through the use of command-line switches:

  • -c: Defragments all volumes on the system.  You can use this switch without needing to specify a drive letter or mount point
  • -a: Perform an analysis of the selected drive and provides a summary output (shown below):

  • -r: Performs a partial defragmentation by consolidating only file fragments that are less than 64MB in size.  This is the default setting
  • -w: Performs a full defragmentation by consolidating all file fragments regardless of size
  • -f: Force defragmentation of the volume even if the amount of free space is lower than normally required.  When running this, be aware that it can result in slow system performance while the defragmentation is occurring
  • -v: Displays verbose reports.  When used in combination with the -a switch, only the analysis report is displayed.  When used alone, both the analysis and defragmentation reports are shown.
  • -i: Runs the defragmentation in the background and only if the system is idle
  • -b: Optimizes boot files and applications, but leaves the rest of the drive untouched

So now that we've covered what disk fragmentation is and how to address it, there are some caveats.  You must have at least 15 percent free space on the disk volume before Disk Defragmenter can completely defragment the volume.  If you have less free space, then a partial defragmentation will occur (unless you force the defragmentation with the -f switch).  Also, you cannot defragment a volume that has been marked by the OS as possibly containing errors.  This is where you would need to use the CHKDSK.EXE utility to ensure that there are no underlying disk issues.  Some other things to look out for:

  • Empty the Recycle Bin before defragmenting.  Disk Defragmenter does not defragment the Recycle Bin
  • As we discussed in our Page File post, if you want to defragment the page file, you need to zero it out first and then defragment the disk
  • By default, fragments that are greater than 64MB in size are ignored by Disk Defragmenter.  Fragments of this size (which already contain at least 16,000 contiguous clusters) have a negligible impact on performance
  • Disk Defragmenter will not defragment files that are in use.  For best results, shut down all running programs, or log off and log back in as an administrative account before defragmenting the disk

And with that, it's time to wrap up this post.  Until next time ...

- CC Hameed

Share this post :

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# BB said on March 14, 2008 9:57 AM:

This post is a good overview of how to use defrag and what to use it for, but I'm interested to know how fragmentation performance issues might come into play when using RAID5 or other disk striping or storage i.e. SAN technology is used.

Do the performance penalties come into play due solely to the seek times of getting data from disparate parts of the disk or are there file system table efficiencies to be gained by defragmentation?  

How would an administrator identify that fragmentation is actually causing a slow down on a system?

# Dwayne's Blog said on March 15, 2008 7:53 PM:

Bit of deja vu here, been a while (or an OS or two) since I discussed defrag seriously. Nice article

# anonymous said on March 17, 2008 4:21 AM:

What is the allocation unit size that MS could recommend for 30-50 GB partitions so that wasted space is minimum yet fragmentation is also minimum?

# someone said on March 17, 2008 4:29 AM:

I can understand about the color coded display being removed, but some sort of progress indicator is a must. Please consider adding at least a basic progress indicator to Windows 7.

# Gisabun said on March 17, 2008 9:30 AM:

I'll assume the version is more specifically for Vista as XP doesn't have as many options. XP's Defrag is also dated from 2001.

# Microsoft Product's said on March 17, 2008 3:38 PM:

When addressing system performance issues, a key element that is often overlooked is Disk Fragmentation

# Realtime Community | Windows Server said on March 20, 2008 2:50 AM:

We all know disk fragmentation isn bad, but cluster sizes on Windows boxes make lots of little pieces out of one big piece. When those pieces get out of order, we see slowdowns. CC Hameed of the Microsoft Performance Team explains the process in this

# Aegis said on March 21, 2008 9:51 AM:

Automatic defragmentation is indeed the best solution to fix this persistent problem. After I installed an auto defragger (third party) I disabled auto defrag for the backups drive, but left it on for the OS and program files partitions. It's awesome since it does a great job yet I dont need to defrag manually or schedule it for midnights. Saves me time and effort. If auto defrag can help even a home  user with just 3 PCs (ok, so one's a laptop), I can understand its usefulness in the corporate sector where its money and labor saving potential is quite high.

# MVE said on April 1, 2008 6:37 AM:

Very nice article indeed!

Next step is how you put it to work in a large organization: how do you start and schedule the job without slowing the computer down and thus disturbing tjhe user at times when it is critical that the user is not disturbed.

# Jeremy Noring said on May 12, 2008 1:54 PM:

Great article.  This was a big help to us when we were troubleshooting some issues with our application and we suspected disk problems.

One quick suggestion: it doesn't appear that the /? shows all of the above flags and settings.  Admittedly I only tried it on XP Pro and not Vista, but it sure would be nice to have all of the help options display.

Thanks again,

Jeremy Noring

# Quark Fusion said on July 1, 2008 1:32 PM:

Do you know what -g switch means? (It's used in ScheduledDefrag task)

# Madhana said on October 3, 2008 9:30 AM:

Hi is there any recommendation of how much Free space (%) is required on C:\ DRIVE ?

Is there any correlation of free space on drive affecting the performance throughput (particularly C:\ Drive) ?

# Terry Ledbetter said on October 7, 2008 8:44 PM:

What about programs running in the background?  I have problems with 'driver changed - restarting' appearing.

Leave a Comment

(required) 
(optional)
(required) 

About CC Hameed

I joined Microsoft as a Support Engineer on the Performance team in September 2005. Prior to that I spent a couple of years working the late night shift on our Platforms 24x7 team. Working for Microsoft was always a dream job - so I am living the dream! I was on the Windows Vista Beta team in 2006, which was one of the coolest projects I have ever worked on, until I took on the task of driving the AskPerf Blog. As you can tell by my logo, I am a huge Manchester United fan and I have successfully managed to brainwash my two daughters into sharing my passion for the Red Devils much to the dismay of their mother! I also coach both my daughters' soccer teams. In addition I am an avid MMO gamer, and have an extensive DVD movie collection.

This Blog

Syndication

Page view tracker