DSA, Database System & Operating System β Memory Management, File Systems & Administration, NEC licence examination syllabus (Nepal Engineering Council).
Impact of Allocation Policy on Fragmentation
How block size and allocation strategy decide how much disk you actually lose β measured.
π Where this lives: the block-size trade-off is a live decision every time you format a volume or configure a database. A build server holding millions of small object files wants small blocks; a video archive wants large ones. Get it wrong on a million-file repository and you lose gigabytes to internal fragmentation β which is exactly why git packs thousands of small objects into single pack files rather than storing each as its own file. Search "ext4 block size small files wasted space" and "git packfile why".
The two fragmentations, in file systems
INTERNAL FRAGMENTATION
space wasted INSIDE an allocated block. A file's last block is
almost never exactly full.
average waste = block_size / 2 per file
MEASURED on this machine (APFS, 4096-byte blocks):
file size blocks allocated waste
ββββββββββββββββββββββββββββββββββββββ
1 byte 4096 bytes 4095 bytes (99.98%!)
100 bytes 4096 bytes 3996 bytes
4096 bytes 4096 bytes 0 bytes (perfect fit)
4097 bytes 8192 bytes 4095 bytes (one byte over)
10000 bytes 12288 bytes 2288 bytes
READ THE 4097-BYTE ROW. One byte past a block boundary costs a
whole extra block β 4095 wasted bytes. That is the sharpest
illustration of why block size matters.
EXTERNAL FRAGMENTATION
free space exists but is scattered in pieces too small to hold
a new file contiguously.
Β· afflicts CONTIGUOUS allocation badly
Β· does NOT afflict linked or indexed allocation, because any
free block will do
Β· but it still degrades PERFORMANCE for indexed allocation: a
file whose blocks are scattered reads slowly, which is what
"the disk is fragmented" popularly means
NOTE THE DISTINCTION: with indexed allocation, external
fragmentation costs SPEED, not CAPACITY. With contiguous
allocation it costs capacity β the write fails outright.
Choosing a block size
THE TRADE-OFF, stated precisely:
LARGE BLOCKS β fewer blocks per file β smaller
metadata, fewer pointers, faster
sequential reads
β MORE internal fragmentation
SMALL BLOCKS β LESS internal fragmentation
β more pointers per file, larger inodes and
bitmaps, more seeks
WORKED CALCULATION β 1,000,000 files averaging 2 KB each,
which is a realistic build cache or mail spool:
block allocated per file total on disk amplification
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
512 B 2048 B (4 blk) 1.91 GiB 1Γ
1024 B 2048 B (2 blk) 1.91 GiB 1Γ
4096 B 4096 B (1 blk) 3.81 GiB 2Γ
16384 B 16384 B (1 blk) 15.26 GiB 8Γ
65536 B 65536 B (1 blk) 61.04 GiB 32Γ
At 64 KB blocks, 1.91 GiB of real data occupies 61 GiB of
disk β a 32Γ amplification. At 512 B or 1 KB it is exact,
because 2048 divides evenly by both.
THE SAME FILES AT 1 MB EACH instead of 2 KB:
512 B blocks β 2048 blocks per file β 2,048,000,000
pointers to track. The metadata becomes
the problem.
64 KB blocks β 16 blocks per file β trivial metadata,
negligible waste (avg 32 KB per 1 MB file
= 3%)
CONCLUSION: the right block size depends on the file-size
DISTRIBUTION, not on the disk. That is why filesystems expose
it as a format-time option and why databases let you choose a
page size.
MEASURED CONTEXT β this machine uses 4096-byte filesystem
blocks but 16384-byte MEMORY pages. Two different
granularities, chosen for two different distributions:
file sizes versus working-set locality.
BLOCK SUB-ALLOCATION β how modern filesystems escape the trade:
Β· FRAGMENTS / BLOCK SUFFIXES (UFS): allow the last partial
block of a file to be shared with other files' tails
Β· TAIL PACKING (ReiserFS, btrfs): pack several small files'
tails into one block
Β· INLINE DATA (ext4, APFS): store a very small file INSIDE its
own inode, using the space the block pointers would occupy.
A 60-byte file then costs ZERO data blocks.
Inline data is why a filesystem full of tiny files can now be
far more efficient than the 4095-bytes-wasted measurement
above suggests β the measurement shows what happens when the
file is big enough to need a block at all.
The 4097-byte case is the one to remember: a single byte past a block boundary allocates an entire extra block and wastes 4,095 of its bytes. It also explains a familiar surprise β a file reported as "4.1 KB" occupying 8 KB on disk. ls -l shows the logical size; du shows the allocated size, and the two differ by exactly the internal fragmentation.
Combating fragmentation
FOR EXTERNAL FRAGMENTATION (contiguous allocation):
COMPACTION / DEFRAGMENTATION β relocate files so free space
coalesces into large runs.
β restores contiguity and sequential read speed
β expensive: it rewrites large amounts of data
β on an SSD it is actively HARMFUL β it causes wear with no
benefit, because an SSD has no seek penalty. Never defragment
an SSD.
EXTENT-BASED ALLOCATION β the modern answer. Instead of
tracking individual blocks, record (start, length) EXTENTS.
ext4, XFS, NTFS, APFS all use extents
β a 1 GB contiguous file needs ONE extent record instead of
262,144 block pointers
β encourages contiguity, since the allocator naturally seeks
large runs
β far smaller metadata
DELAYED ALLOCATION (allocate-on-flush) β do not choose blocks
when write() is called; wait until the data is actually
flushed, by which time the total size is known and one large
contiguous extent can be picked.
β dramatically reduces fragmentation for files written
incrementally
β a crash before the flush loses more data, which caused the
well-known ext4 "zero-length file after crash" complaints
PREALLOCATION β the application declares the final size up
front:
fallocate() / posix_fallocate()
Databases and torrent clients do this precisely to get one
contiguous extent.
FOR INTERNAL FRAGMENTATION:
Β· smaller blocks (with the metadata cost)
Β· tail packing / fragments
Β· inline data in the inode for tiny files
Β· at the application level: PACK many small items into one
large file, which is what git packfiles, JAR/ZIP archives
and database tablespaces all do
THE UNIFYING OBSERVATION: file-system fragmentation and memory
fragmentation are the same problem with the same solutions.
contiguous allocation β contiguous memory allocation
indexed allocation β paging
extents β huge pages
compaction β memory compaction
Fixed-size units eliminate external fragmentation and create
internal fragmentation; variable-size units do the reverse.
There is no third option.
π Go further: SSDs add a fragmentation problem the classical model does not cover: write amplification. Flash can only be erased in large blocks (megabytes), so overwriting 4 KB may require reading, erasing and rewriting a whole erase block. The Flash Translation Layer hides this, but it means the drive does its own internal "defragmentation" (garbage collection) invisibly, and that TRIM β telling the drive which blocks are free β materially affects performance and lifespan. Search "SSD write amplification TRIM garbage collection".
π‘ Exam angle: distinguish internal fragmentation (waste inside an allocated block, averaging half a block per file) from external fragmentation (free space too scattered to use). Know that indexed and linked allocation eliminate external fragmentation but not internal, and that with indexed allocation scattered blocks cost speed rather than capacity. The block-size trade-off calculation is a standard numerical: given a file count and average size, compute total allocation at several block sizes. Mention extents, delayed allocation and tail packing as modern mitigations, and that defragmenting an SSD is harmful.
Syllabus points
Allocation policy vs fragmentation
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Memory Management, File Systems & Administration