Back to Home

Sparse files in NTFS

NTFS has support for sparse files. These are files that take up less disk space than their own size. This technology is not related to ...

Sparse files in NTFS

    NTFS has support for sparse files. These are files that take up less disk space than their own size. This technology is not related to the support of file compression built into NTFS, since saving disk space in sparse files is based on a different principle. No data compression is performed. Instead, areas occupied by zeros only (0x00) are freed in the file. An application that reads a sparse file, reaching an area with zeros, reads zeros, but does not actually read from disk.

    This way you can create gigantic files, consisting of zeros, but on the disk they can occupy only a few kilobytes. Real disk space is allocated when some other data is written instead of 0x00. Sparseness will help save disk space only in files that have really large empty areas.

    I will demonstrate working with sparse files using the fsutil system utility on the command line.

    Using the utility, create an empty large file:
    fsutil file createnew test.nul 10000000000

    Assign the “sparse” attribute to the file:
    fsutil sparse setflag test.nul

    The attribute itself does not yet save disk space. You also need to mark the area inside the file that will be freed. We have the entire file empty, so the area can be set to the file size.
    fsutil sparse setrange test.nul 0 10000000000

    Done. We look at the result.
    image

    You can see how to do these operations in your programs using the API functions in the source code of a small utility that I wrote in 2006. It is also a console, and also like fsutil can assign the sparse attribute and set the range of the freed area. In addition, my program can search for empty areas in a file larger than a certain specified size and free them. There is no need to calculate the offsets yourself.

    References:

    Read Next