What does a defrag do really? Does it put the files that execute in sequence closer together?
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).
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.
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
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.
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.
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.
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.
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.
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!