Jump to content

File Fragmentation/Defrag


Recommended Posts

Posted

Win XP Home SP.3.

I know what causes fragmentation and that a Defrag utility will, to a great extent, sort it out but I don't know how Defrag works.

Does Defrag read file fragments into ram then write them back to disk contiguously? What will happen if a fragmented file is read but there isn't a contiguous area on disk to which the file can be written, is the newly written file then fragmented?

I assume that the areas used by file fragments will not be made available for re-use until the new defragmented file is written so a reasonable percentage of free disk space must be available to Defrag, what would that percentage of total disk space be, or is that reliant on the size of the Used disk space??

Thanks - I have searched for info and found lots on what causes fragmentation but very little on how Defrag achieves defragmentation.

V.


Posted

First, the MSDN description of how to defragment a volume via code - these are the functions most defragmenters for windows use when defragmenting a volume, so at least understanding that these exist will be a good start. Obviously these APIs only provide information about the filesystem, it's up to the app that's calling them to determine what to do with a file or cluster, etc. Note it goes FAR deeper than what I'm about to skim over (DeviceIOControls, IOCTLs, GetFileMap, GetVolumeMap, etc), but this is just a very, very basic high-level overview of how it "works".

Ultimately, your answer really comes down to how the defrag program is configured to run - some try to put all of the files in a contiguous area, some try to do other things, etc. As to what happens with the file, generally the MoveFile API is used to move a file (and it's fragments) once it's been targeted to "defrag" to move it to another area of disk, and yes this means files are generally copied into and out of the running defragmenter's process address space, which usually means RAM (but could be paged to disk, technically, if needed, but that's likely not a normal scenario).

In general, most apps will say at least 15% of the drive needs to be available for defragmentation, but I've found it's more like 25% (and that free space can't be heavily fragmented either, or it's extremely hard to move files around). In general, if you've less than 25% free space on a drive, defragmenting it becomes difficult. Not impossible, but difficult (and may require your defrag program to take multiple passes at a file system to get it "defragged", etc).

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...