What does a defrag actually do?
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
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.