| • Science | • People | • Locations | • Timeline |
Fragmentation occurs both in memory allocation in RAM and in secondary storage devices such as hard drives.
Fragmentation is of two types: internal and external. In general, internal fragmentation results from only partially filling a reserved piece of space, while external fragmentation refers to the unallocated space left between portions of allocated space, particularly when this space is not useful, for example because it's too small. Defragmentation utilities can only get rid of external fragmentation. Internal fragmentation is generally identified and removed by data compression utilities.
On a hard drive, files always start at the beginning of a sector. Any space left over between the last byte of the file and the first byte of the next sector is internal fragmentation. Operating systems also try to use consecutive sectors for the contents of each file, because it's faster to read a file when all of its sectors are consecutive than when they are scattered all over the disk. Sectors between files, which contain no useful data, are external fragmentation.
Letters are often stored in 8-bit bytes, even though in many character strings the 8th bit of each byte is always zero. The "wasted" bits are internal fragmentation.
In dynamic memory allocation, when a program requests a block of memory that is bigger than any available space, it is denied the request, even though more than enough space may be available in total. The program has fallen victim to external fragmentation. Similarly, a program which allocates a single byte is often allocated many more bytes, to use for metadata and alignment. In this case, the extra space is internal fragmentation.