The received wisdom is that defragmenting solid state media such as CompactFlash and SD flash memory cards is unnecessary, at least that's what I'd always heard. With magnetic hard drives, fragmentation hurts performance because of the time it takes to physically move the drive heads around when a file is non-contiguous, but since flash memory does not have to move drive physical heads around, it makes sense that any performance loss due to fragmentation would be extremely minimal. However, it was claimed in a usenet post that in fact major benefit could be derived by defragmenting Pocket PC flash storage, but since the author of that controversial claim had not (yet) published any data to support it, I decided to investigate myself.
I carried out this test on my trusty WM2003 iPAQ 3850, with a 256MB SanDisk CF card in the expansion pack CF slot. Yes, this is old gear, but you would expect the relative trend to hold true for newer hardware - feel free to confirm it though. The CF card is formatted as FAT32, 1kB clusters. I put the card in my card reader, wiped it, and induced fairly severe fragmentation on it using frag, a little program I wrote for the purpose. After letting frag run for fifty passes, it left the card filled with random sized files (1kB up to ~25MB) and heavily fragmented, according to the Windows Defrag utility. I then put the card back in the iPAQ, and benchmarked it using the SiSoft Sandra* PDA removable storage benchmark, first in the fragmented state, then in the defragmented state, and finally with the card blanked. For good measure I also stopwatch-timed a copy of a folder of several hundred small files (up to ~10kB) from main memory to the card, in the same three states.
Here are the Sandra benchmark results. The vertical axis on each graph shows operations per minute (note: on a log scale, to better show the relative trends). The horizontal axis is test file size.
If like me you thought that flash memory wouldn't be affected by fragmentation, then you'll find these results quite an eye opener. Looking at the write performance, you can see that while there is no difference in performance between the card states for 512B files (as you'd expect, since they'll fit in a single block, and therefore won't ever be fragmented), for 32kB files, the fragmented card has dropped to half the performance of the defragmented and blank cards. By the time you hit 256kB files, the fragmented card has dropped to almost one quarter the performance of the defragged card, and one eighth the performance of the blank card! The relative performance seems to be maintained at the same level for 2MB files as for 256kB files. With read performance, the difference doesn't get huge until the 2MB range, but then we see a massive drop in performance.
So what does this mean in the real world? Some of those performance losses are big! It depends very much on your usage profile as to how much it actually affects you, but for example a web browser is bound to be caching images in the 10s or 100s of kB range on a regular basis, so that seems a likely example of an application that will definitely suffer noticeable performance loss due to fragmentation. As the man said, "Even flash ROM's benefit a LOT from defragging."