I’ve written before on this site about using encrypted sparse disk images to store private information. Over time, as things are deleted from those disk images, they can become fragmented, and the image contains blank space between files, which can cause the image file itself to be unnecessarily large. For example, if you have two text files that are each 32K, and 1GB of blank space in between them on the disk, your sparse image will be 1GB in size and only contain 64K of actual data. That’s quite a chunk of slack space. Reclaiming that space is easy, but it may require that you drop a little bit of cash to do it.
The image needs to be defragmented and then compacted. To my knowledge, there is no freeware disk defragmenter for OS X. I use iDefrag ($30) and it does the job well for me. You’ll want to mount the image on the desktop, run iDefrag, and perform a full defragmentation. That will compact all of the data in the image’s filesystem and move it to the beginning of the image.
The next step requires our good friend the Terminal. After defragmenting the image and dismounting it, start /Applications/Utilities/Terminal.app and enter the following:
hdiutil compact /path/to/image.file
This is a different kind of compaction than what the defragmenter performs. In this step, hdiutil scans the image for the last point where data exists, and truncates the empty space after it, reducing the size of the image file.
To continue our example above, iDefrag would move our two 32K files to the beginning of the image’s filesystem. Then hdiutil would slice off the remaining 1GB of empty space after those files, reducing the image’s size to megabytes instead of gigabytes. The size of the final image will vary because there is a certain amount of overhead for filesystem structures.

My .Mac Web Gallery
Recent Comments