MacTip #32, Fragmentation
There seems to be one area of hard drive maintenance that often takes the back seat and is often misunderstood. This MacTip discusses the role of fragmentation in the functioning of your Mac, how to avoid it, and what to do about it.
Fragmentation is the process in which files on your hard drive get broken into 2 or more pieces. Although fragmentation is a part of the normal working on your hard drive, it can at the least cause slower performance, and in severe cases cause stability problems or even data loss.
What you can't see...
Most of what we will discuss in this MacTip has to do with things that you would never see in day to day use of your Mac. Our discussion of fragmentation has to do with the physical position of the data on your hard drives.
All the information on your hard drive is digital. Digital simply means that in its smallest part, the information can be broken down to a bunch of ones and zeros. Although you may have all your files organized into various folders in the Finder, this organization really has little to do with how all those ones and zeros are physically laid out on your hard drive. Generally when a file is saved to your hard drive it will be saved all in one contiguous line of ones and zeros if possible. Fragmentation happens when it is not possible to place all those ones and zeros in a single line but rather have to separate it into two or more parts.
Fragmentation is a good thing.
Before we get too excited about the possible negative effects of fragmentation, it is helpful to understand that fragmentation is a part of the normal working of your Mac. Fragmentation allows you to save large files even if you don't have enough contiguous space on your hard drive and also allows you to add information to files already on your drive without having to move them to a spot where there is more room.
How it happens.
As mentioned above fragmentation is a part of the normal functioning of your hard drive. Each hard drive (or partitioned volume of a hard drive) is a single contiguous place for storing data. If you were to simply copy 10 files onto an empty hard drive each one would be placed directly after the last. Adding one more file (the red one) causes no fragmentation.
Fragmentation occurs either when the file being saved is larger than the next available space, or when a file no longer has any room to grow and the additional information is saved in a separate location.
The first situation occurs more often when a volume is nearing full capacity. Although you may have enough free space on your drive to save a certain file. All that space may not be in a single contiguous line. Your hard drive may look something like below.
When a file is saved, it is forced to be saved in the available space between the files that are already on the drive. In the sample below you can notice that the red file had to be saved into 7 pieces.
Fragmentation allows that file to be broken up into separate pieces and placed on your hard drive. The more full a hard drive is, the more likely that there will not be a single contiguous spot to place the information. Therefore, if your hard drive is always running with only a few megabytes free, odds are you are forcing files to be separated into multiple fragments as they are being written to your hard drive.
The second and more common manner in which fragmentation occurs is when a file no longer has more room to grow. Part of the design of the Mac OS involves leaving a small amount of space after each file in case it needs to grow. Although this small amount of space works to decrease the likelihood of a file needing to become fragmented it only reserves enough room for the file to grow slightly (usually between 2k and 64k depending on the size of your hard drive.).
Let's walk through a possible example. Imagine you are in the process of writing a journal of your life. You sit down one night and write just a few lines each night and save it out to the hard drive (The red one). The file, when you first save it might be about 2k. If you have an allocation block of 8k you can add to your journal quite a bit before you fill the initial 8k that was reserved for the file when you first saved it.
Over time your Journal file will increase in size and you will also be saving other files to your hard drive. If you create other files, download items from the internet or install software, you are lock that initial file into the initial 8k spot that it reserved for yourself.
As you continue to record the events that happen in your life your journal file will need to grow larger than that 8k and it will reserve another 8k spot for itself. Once that happens, the file is spread between two 8k physical locations on your hard drive.
If this process continues, your journal file will continue to stake out areas of your hard drive in 8k chunks.
I'm sure you can imagine that over time you could end up with tens or hundreds of little pieces of this file scattered all around your hard drive. Each time you open your journal file, your computer needs to go collect each of those pieces and load them into memory. Files that grow a little bit at a time on a regular basis are ones the tend to become more fragmented. Generally these files are ones like databases that are added to regularly. If you have a database where you store names and addresses and frequently add to it, it may become fragmented over time. The files on my hard drive that tend to get more fragmented faster than any others are the ones that store my email. Each and every day I get a fair amount of email that are added to the email databases for Claris Emailer. At the same time I'm also downloading other files, working on the web site, writing letters and doing various other things with my computer. This leads to those particular files being segmented into many little pieces. (Last time I optimized my hard drive, the two files that constitute my email databases were spread over 900 separate different locations on my hard drive!)
HFS+ and Fragmentation
Apple's NEW Extended disk format (HFS+) increases the likelihood of your files becoming fragmented.
One of the primary advantages of HFS+ is that it changes the way that information is stored on your hard drive. The most significant factor related to fragmentation is what is called the "Allocation Block". The size of the allocation block determines how much drive space a file can take up. If your drive has an allocation block size of 32k, a file with only 1k of information will occupy 32k of disk space. Likewise a file of 35k will occupy 64k of disk space because of the 32k allocation block. Although this may not facilitate the most efficient use of your drive, it does allow that those files some room to grow without having to be broken into multiple pieces on the drive.
Prior to HFS+ the allocation block was determined in direct relation to the size of your hard drive. So a 2 gigabyte volume would have a allocation block size of 32k and a 4 gigabyte drive would have one of 64k. (See the chart in the middle of MacTip #10) HFS+ changes the way the allocation block size is calculated. With HFS+ Allocation blocks generally run between 2k and 4k. Although this is a much more efficient use of space, in general files don't have as much room to grow and need to be fragmented into multiple pieces sooner than with standard HFS.
Understanding what causes fragmentation will help you to take the unseen factors into account when using your Mac.
This site, its source code, and the MacTips logo ©1996-2016 Eric Prentice. All other names, logos and images are copyright their respective owners.