Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What does a defrag do really? Does it put the files that execute in sequence closer together?

share|improve this question

8 Answers 8

Actually it puts all the different parts of ONE file together, so if that particular file is needed, the harddisk doesn't have to move its arm (which is the real performance-breaker here) that much to gather all the bits of the file. ==> hence, better perfomance (although it's not always noticable).

Note that this only applies to hard disks with platters and movable parts. For SSD's this is no problem, as it takes the same time to fetch any data independent from where it's located on the disk. Defragging SSD's is actually a bad idea as they have a limited number of times they can be written to on each 'sector' (I don't think 'sector' is the right term here, but you know what I mean I hope).

share|improve this answer
2  
Not only is it not a problem for SSD's but defragging them is actually a bad idea due to their limits. –  Jeff Yates Jul 27 '09 at 14:13
    
Indeed I'll update my answer. –  fretje Jul 27 '09 at 14:29

It depends on the defragmentation program.

At a minimum it will aim to put the clusters for a single file or directory into a contiguous sequence, providing that there is space available to do this.

It may also sequence files and directories according to usage profiles, if it collects these.

It may not be able to move some files (certain open and system files typically).

The reason for defragmenting is that random reads are considerably slower than reading a continuous sequence of blocks/clusters off the disk. However, not all file systems or operating systems have the same requirements for (separate) defragmentation programs.

share|improve this answer
    
Defragmentation also usually groups the free space on the disk into clumps that are as big as possible. This helps to prevent newly-created files from being fragmented in the first place and allows them to be written out faster. –  David Schwartz Aug 24 '11 at 2:37

Defragging can do two things:

  • Ensures all the pieces of a file are located in a continuous part of the hard drive. Most files are split into smaller "fragments" that are stuck wherever there is space. At first they will be stored continuously, but gaps/incontinuities will be created when files are deleted/modified. If a file is continuously stored, then it can be read faster.
  • Optimize the location on the harddrive (spinning harddrives only.) Hard drives are composed of spinning disk-shaped platters. The outside spins faster, so the harddrive will perform better when writing/reading near the outside of these platters because more data will be passing under the read/write head for a given time period.

Also, I'd like to add something about defragging SSD's:

Defragging an SSD

Long ago I promised to post on why defragging an SSD is still relevant and necessary. It’s true that with an SSD there are no physical platters that have to spin into place and no read/write arms that have to move wildly across the surface of a disk. For this reason, it seems like defragging is a waste of time. You could also argue that since SSDs eventually wear out, you should not waste rewrite cycles on something as simple as defragging your disk.

One of the facts about NAND flash memory technology used in SSDs is that writing data takes much longer than reading data. When a file is erased, much like standard hard disk technology, only the directory information is erased and the data cells are marked “usable” again. The data itself is still intact until something new overwrites it.

This means that if the memory space is in use with a “deleted” file, it must first be erased before it can be reprogrammed. If the free space is fragmented, a new file must be broken into several small write commands instead of one larger write command. This will cause a performance decrease for SSDs because write speed is slow, especially for small block write transfers. Yes, cache memory helps mask this performance issue, but no matter what the technology, one long write command is always better than multiple short write commands.

Net, defrag your disk regularly if you want maximum performance.

source: Inside the Box Lenovo blog

share|improve this answer
2  
This guy you quote is wrong. The SSD internally combines several writes to different logical blocks into a single flash write. Like reads, random writes are just as fast as sequential on an SSD. –  psusi Jan 13 '11 at 20:29
3  
You also can't control where data is placed on most SSDs, due to the wear leveling algorithms. While it may be possible to overcome these algorithms with the use of special software, most "off-the-shelf" software won't cut it. –  Breakthrough Aug 18 '11 at 15:01

Without getting into too much technical mumbo jumbo, it makes your hard drive look less like swiss cheese.

More details and an animated image here: http://en.wikipedia.org/wiki/Defrag

share|improve this answer

Defragmenting a drive can do a couple of different things. Typically the defrag process looks for files which have been broken up into chunks for whatever reason. Many cases this occurs from reading and writing to a drive over time. The defrag program assembles all of the fragments and then find a spot on the disk where that entire file will fit. In some cases it ends up being a but of a shell game to move things around making places for all the largest files.

Now something to be said about putting all of these files together concerns the typical "seek" time of the Read/Write head on the disk. Every time you have to move the read/write head somewhere else on the disk you typically incur a delay of a number of milliseconds. When reading a file that is all in one location, the read/write head does not have to move very far at all to continue reading the file. This is much faster than going to another spot entirely on the disk. In some cases defragmentation can be done to optimize the operating system by placing the system files accessed most frequently closer to the "landing zone" for the read/right head when it is not performing any reads/writes. This can in many cases speed things up quite a bit.

I have not seen a defrag tool out there which has the ability to keep statistics on the number of times a file has been accessed, but if there was, it could stack the disk first with the most frequently accessed files.

share|improve this answer

Hardware

As others have mentioned, it ensures that files are stored in contiguously numbered allocation units.

On spinning media like hard-drives, this provides faster access to the data since the platter does not have to spin more and the heads do not have to thrash back and forth. On SSDs, this has no performance benefit.

Data Recovery

One thing of note about defragmentation is that by putting files in consecutive allocation units, data-recovery becomes easier and more likely to succeed. When you accidentally delete a file, have a drive crash, or are hit by a virus, fragmented files are much less likely to succeed.

This is because if the file is split into multiple pieces that are scattered all over the drive, then it becomes difficult (often impossible) to figure out which parts go where and belong to which files. This is even more problematic with larger drives containing more files. To successfully recover a fragmented file, you need to know where each part of it is and the order of them, but if you had that, you wouldn’t be needing to recover it.

If files are not fragmented, then all you have to do is to find the beginning of the file and either know the size, or know how to detect the end (e.g., by knowing the format of the file, or detecting when a new file begins).

One way to help with data recovery is to use a file-system that is more tolerant of corruption and contains better backup and recovery functions such as NTFS over FAT32. Unfortunately, no file-system is perfect and even the best ones are still subject to data loss and require data-recovery now and then.

Recommendations

With a hard-drive, ensuring that the disk is defragmented (e.g., having it scheduled to automatically defrag whenever the system is idle—which becomes less and less of a task as it cleans things up) helps improve your chances of recovering lost files. Since SSDs are not suited to being defragmented, you will need to weigh the risks and benefits of defragging and either wear out the drive, make regular backups, or store critical data on a hard-drive.

share|improve this answer

Your file system will sometimes put parts of a file in different locations on the disk. When accessing this file later, it takes longer because it has to go to each of those locations to access the file. That is called fragmentation. Basically, you have fragments of your files in different places on the disk.

When you defragment the disk, you are rearranging the files on the disk so that as many files as possible are stored in their entirety.

share|improve this answer

It depends on the file system, the defragmenter, and the HDD/SSD.

Most files will be consolidated into one block each. That's the minimum requirement and definition of "defragmenter".

More advanced defragmenters will move directories first (since they are, on a per-megabyte average, accessed more frequently than ordinary files), followed by files accessed recently. Files which have not been accessed for a long time (typically 90 days) are sometimes actively placed closer to the end, to allow for shorter seeks for files which do get accessed.

NTFs seems to fragment only slightly less than FAT but to degrade more slowly if it is. Some of the important file are not at the front of the partition but near the middle, obviously to cut the worst case access time in half. Not sure about it, though.

The "seek" times on SSDs are minimal, on the order of microseconds. You're looking at the wrong places if you defrag SSDs for seeks. (An intelligent SSD controller could merely update WHERE the data are moved, rather than moving them, each time it detects the "read old data + wite same data + TRIM old data" pattern, economizing on both write cycles AND defrag runtime. Not sure if any controller does that yet.)

On a second thought, yes, SSDs distribute writes across chips on purpose, because that's how they are faster, and heavy fragmentation might slow the write patterns by making the writes LESS scattered. That might be a factor to consider if it's true.

Always, and regularly, do your backups. If you defrag disks for ease of data recovery, YOU'RE DOING IT WRONG!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.