Flash Foundations companion · Simulator
Defrag Lab
Defragmenting was a fix for hard drives. Scroll to see what it does to the blocks of a simulated SSD, then try the fixes that do work.
Five things defrag does
Illustrative model, not vendor data. Trends are real, numbers are not.
Defrag helps a hard drive, not an SSD
A hard drive pays a seek and half a turn of the platter for every fragment, so a tidy file reads much faster. An SSD has no arm to swing, and defrag tidies the addresses, not the chips.
What to look at. Look at the picked file. On the volume it becomes one run, but its pages on the drive stay spread across the dies.
- Picked file
- Other data
- Free
The volume, as the computer sees it
The drive, as the chips hold it
The picked file went from 6 fragments to 1. The same reads got 3% faster on an SSD and 15% faster on a hard drive.
A full defrag costs NAND writes
A defrag copies data from one place to another, and every copy is a NAND write that wears the flash. Garbage collection already moves data inside the drive on its own.
What to look at. Look at the drive. The pages the defrag wrote are new live pages, and the old copies are now stale.
- Live page
- Stale page
- Free
The volume, as the computer sees it
The drive, as the chips hold it
Reads on an SSD got 3% faster for 286 NAND pages written.
Defrag only the data that split reads
A fragment costs extra only when it adds a command. FragPicker, a research tool from 2021, moves only the data that split real reads.
What to look at. Look at the picked file, which improved most, and at the page counts below. Far fewer pages moved than in a full defrag.
- Picked file
- Other data
- Free
The volume, as the computer sees it
The drive, as the chips hold it
The picked file went from 5 fragments to 2. It wrote 79 pages and the traced reads went from 19.4ms to 18.7ms on the SSD. A full defrag of the same volume writes 274 pages and reaches 18.7ms.
What Windows actually does
Windows tidies an SSD on a schedule, mostly with ReTrim. It tells the drive again which space is free and moves no data.
What to look at. Look at the dotted squares on the volume. ReTrim clears them, and the same pages turn stale on the drive.
- File
- Free
- Free, drive holds data
- Live page
- Stale page
The volume, as the computer sees it
The drive, as the chips hold it
ReTrim told the drive about 256 free blocks and wrote 0 pages. The dotted squares went from 256 to 0.
Microsoft Learn says the weekly Optimize Drives task, on an SSD, defrags and retrims once a month. A 2014 comment by a Windows developer, quoted by Scott Hanselman, says the defrag runs only when snapshots are on. What it leaves out covers the gap.
Remap, and place data by lifetime
Two ideas go further than tidying. One fixes addresses without copying data, and the other keeps short-lived and long-lived data in different blocks.
What to look at. Remap is a research idea from 2017, and standard NVMe has no command for it. Look at the drive: the volume is tidy, yet no page moved.
- Picked file
- Other data
- Free
The volume, as the computer sees it
The drive, as the chips hold it
The picked file went from 6 fragments to 1. Remap wrote 0 pages to the NAND. A copying defrag of the same volume writes 699.
What to look at. ZNS and FDP let the host ask for this. Look at the two drives after the same week of hot and cold writes, then at the write amplification below.
- Live page
- Stale page
- Free
The drive after a week of hot and cold writes
With one write stream the week's write amplification was 1.78. Placed by lifetime it was 1.59.
The sandbox
One volume you control
Change a setting, press a Run button, and watch both maps and the chart. Reset rebuilds the volume.
Rewrites every split file into one run of addresses.
Volume settings
Aged: three quarters full after months of use. Hot and cold: mixed lifetimes. Snapshots on: System Restore is on.
On: deleting a file sends TRIM, so the drive drops that data. Off: the drive keeps copying it until the block is rewritten.
More settings
In this model, on: the Windows monthly pass also defragments the most split files. Off: it only runs ReTrim. The on setting follows a 2014 statement by a Windows developer. Microsoft's current documentation lists the SSD defrag without a snapshot condition, so treat this switch as a what-if.
The lab makes up its files at random. The same number gives the same files every time, so a copied link shows what you saw.
The volume, as the computer sees it
- File
- Free
- Free, drive holds data
- Picked file
Files as a table
| File | Blocks | Fragments | Extents (start+length) |
|---|
The drive, as the chips hold it
- Live page
- Stale page
- Free page
- Picked file
Dies as a table
| Die | Live pages | Stale pages | Free pages | Block erases |
|---|
The bench: pages written by each run
The first four bars are each way to tidy this volume, run once on a fresh copy. Your runs follow. The axis grows to fit the longest bar.
- NAND writes
- Host writes
Table for the writes chart
| Run | Writes |
|---|
Counters
How it works
A volume of 1,024 blocks of 4KiB sits on a small page-mapped drive: 8 dies of 20 blocks of 8 pages, so the flash holds 25% more than the volume needs. Files are lists of extents, and each extent is one fragment. Defrag changes the extents and rewrites the data. The drive writes every page somewhere new and stripes writes across the dies, and its L2P map says where each address went. Writes draw on a free block pool.
A read of a file is split at every fragment boundary, and again at the maximum transfer size. A hard drive pays a seek and half a rotation for each fragment. The SSD pays a fixed overhead and the flash read time for each command, and reads the pages of one command from the dies in parallel. Real drives hide their layout, so no tool on the host can see which die holds a page. The reads in each run come from a seeded access trace.
Show the numbers
- Hard drive read
t = n × (seek + half a turn) + bytes / rate8.5ms average seek and 4.16ms for half a turn at 7,200rpm, from a Seagate BarraCuda manual. 210MB/s, the data sheet's maximum sustained read rate on the outer tracks.- Commands
commands = ∑ ceil(fragment / MDTS)Each fragment is cut at the maximum transfer size. A fragment that is a multiple of it adds nothing.- SSD read
t = ∑ (overhead + tR × ceil(pages / 8) + bytes / link)10µs per command and the 3.5GB/s link are model values. 74µs per page read is the typical single-plane read time on one 3D TLC data sheet. Commands run one after another at queue depth 1.- Write amplification
WA = NAND writes / host writesNAND writes are host writes plus the pages garbage collection copies.- Cost of cleaning a block
copies = valid pages in the victimGreedy garbage collection picks the block with the fewest. Fuller volumes leave more live pages in every block.
What it leaves out
- The flash holds 25% more than the volume, and a block has 8 pages. Real drives have hundreds of pages per block, planes and channels, and different spare space.
- This model counts only the cost of extra commands. A 2024 FAST paper found that on real SSDs the larger cost of fragmentation comes from reads landing on the same die (die-level collisions), which this model does not simulate.
- One mapping unit of 4KiB per page, no write cache, no SLC cache, no DRAM, no read disturb, retention or error correction.
- No file system metadata, journal or snapshot data. The snapshots switch only turns on the Windows monthly pass.
- The Windows monthly pass is a model. Microsoft Learn says the scheduled task defrags and retrims an SSD once a month, with no snapshot condition, and does not say what that step moves. A reader comment quoting a Windows storage team developer, in Scott Hanselman's December 2014 post says the defrag happens when volume snapshots are on. The two sources disagree on that condition, and the System Restore on switch follows the 2014 one. Microsoft publishes no fragment threshold, so the lab's threshold of 6 fragments is a model value.
- Command overhead, link speed and the default maximum transfer size are model values. The NVMe specification sets no default transfer size: each drive reports its own, and 0 means no limit. Queue depth, read-ahead and caches change the absolute numbers on a real drive.
- Remap is a research idea (janusd, 2017) that needs custom drive commands. The NVM Command Set specification (revision 1.2, 2025) lists no remap command. FragPicker, ZNS and FDP are modeled from their papers and specifications, and the lab's numbers for them are model values. The published benefit of FDP on cache workloads is larger than this model shows.
- Wear leveling only prefers the least-erased block. One namespace.
- Every constant is a teaching value. The sources and method say where each one comes from.