Sparse file

On this page 7

A sparse file is a file in which large ranges of zero-filled data are not physically stored on disk; the file system records those ranges, called holes, as metadata and returns zeros when a program reads them. As a result, the logical size of a sparse file can be far larger than the space it actually occupies.

In operating systems, sparse files are a standard feature of modern storage stacks, supported by NTFS, ext4, XFS, Btrfs, ZFS and APFS. They are widely used for virtual machine disk images, database files, download preallocation and log or index files that are written at scattered offsets.

How a sparse file stores holes

A file system maps each file onto allocation units or blocks through an extent list or block map. In an ordinary file, every block in the file’s range is allocated. In a sparse file, the map simply contains no allocation for some ranges, and the file system treats any unmapped range inside the file size as a hole that reads back as zeros.

Holes are usually created in one of three ways. A program can seek past the end of a file and write there, leaving the skipped range unallocated. It can explicitly punch a hole into existing data, for example with fallocate using the punch-hole flag on Linux or FSCTL_SET_ZERO_DATA on Windows. Or a copy tool can detect runs of zeros and skip writing them. On NTFS, a file must first be marked with the sparse attribute before holes are honored.

Hole granularity follows the file system block size, typically 4 KiB. A run of zeros smaller than one block, or not aligned to block boundaries, is still stored, so sparseness saves space only for block-aligned zero ranges.

The table below lists the main operations that create or inspect holes.

OperationPlatformEffect
Seek and write beyond endAll major systemsSkipped range becomes a hole
Punch holeLinux (fallocate)Deallocates an existing range
Set zero dataWindows NTFSReleases blocks in a sparse-marked file
SEEK_HOLE and SEEK_DATALinux, macOS, BSDLocates holes and data regions
Query allocated rangesWindows NTFSLists the ranges backed by storage
Key operations on sparse files

Logical size versus allocated size

Every sparse file has two sizes, and confusing them is the most common source of surprise. The apparent size is what ls -l or the Windows Size field shows; the allocated size is what du or the Windows “Size on disk” field shows. A 100 GB virtual disk that contains 12 GB of written data may report 100 GB apparent and roughly 12 GB allocated.

The following table shows how common tools report each value.

Tool or viewValue shownCounts holes
ls -lApparent sizeYes
duAllocated blocksNo
du –apparent-sizeApparent sizeYes
Windows SizeApparent sizeYes
Windows Size on diskAllocated clustersNo
stat (blocks field)Allocated 512-byte unitsNo
Where each size appears

Because applications see only zeros, a sparse file is functionally identical to a fully written file of the same content. The difference exists only at the storage layer, which is why it can disappear silently when the file is copied by a tool that does not understand holes.

Sparse files belong to a family of techniques that decouple logical capacity from physical usage. They are often mentioned alongside thin provisioning, compression and copy-on-write cloning, which share the goal of saving space but work differently.

TechniqueWhat is savedData it applies toLayer
Sparse fileSpace for zero rangesBlock-aligned zeros onlyFile system
Preallocated fileNothing, space is reservedAny dataFile system
Thin provisioningUnused volume capacityUnwritten volume blocksVolume or storage array
Transparent compressionSpace for repetitive dataAny compressible dataFile system
Copy-on-write cloningSpace for duplicate blocksShared identical blocksCopy-on-write file system
Sparse files and neighboring techniques

The contrast with preallocation is important. A preallocated file reserves real blocks to guarantee space and reduce fragmentation, while a sparse file deliberately defers allocation until data is written. The two serve opposite goals, and choosing the wrong one can cause either wasted space or failed writes.

Where sparse files are used

Sparse files are most valuable when a file needs a large fixed address range but only part of it is ever written. Typical cases include:

  • Virtual machine disk images in raw format, which present a full-size disk to the guest but consume only written blocks.
  • Database and index files that reserve space at fixed offsets for later records.
  • Download clients that create the full-size target file first and fill pieces in arbitrary order.
  • Core dumps and memory snapshots, which often contain long unused regions.
  • Disk image backups produced by tools that skip unused areas of a partition.

Support differs by file system. FAT32 and exFAT do not support sparse files, so copying a sparse file to a removable drive formatted with them writes every zero block in full.

Risks and common mistakes with sparse files

The flexibility of sparse files introduces operational risks that administrators must plan for. The most serious is overcommitment: several sparse files can together claim more logical space than the volume holds, and writes then fail with a disk-full error at an unpredictable moment.

The table below summarizes typical problems and remedies.

SymptomCauseFix
Copy fills the target diskCopy tool writes holes as zerosUse cp --sparse=always or rsync --sparse
Sudden write failureVolume overcommitted by sparse filesMonitor free space, set disk quota
Slow sequential readsFragmentation from scattered writesPreallocate hot files, defragment
Archive is hugeArchiver ignores holesUse a sparse-aware archive option
Quota looks wrongApparent and allocated sizes confusedCompare du with apparent size
Common problems and fixes

Fragmentation is a quieter cost. Because blocks are allocated only when written, often in random order, a heavily used sparse file can end up scattered across the disk, which hurts performance on hard drives more than on solid-state drives.

In plain words

Imagine a notebook with 1,000 pages where you have written on only 20 of them. Instead of carrying the whole notebook, you carry the 20 written pages plus a note that says “all the other pages are blank”. Anyone who asks for page 500 is simply told it is empty.

A sparse file works the same way. The computer stores only the parts that contain real data and remembers where the empty parts are. The file looks big, but it takes up much less disk space.

A real-world example

A systems administrator at a small software company runs eight test virtual machines on a Linux workstation with a 1 TB SSD. Each machine uses a raw 120 GB disk image created as a sparse file, so the logical total is 960 GB. After the guest systems are installed, du shows that the images together occupy only about 210 GB of real storage.

Trouble appears when the administrator copies the images to a backup drive with a plain file copy and the target fills up, because every hole is written out as zeros. Switching to rsync --sparse restores the original footprint. The administrator also adds a free-space alert at 80 percent so that growing guests cannot overcommit the SSD without warning.