ⓘ Sparse file. In computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when the file itself is parti ..

                                     

ⓘ Sparse file

In computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when the file itself is partially empty. This is achieved by writing brief information representing the empty blocks to disk instead of the actual "empty" space which makes up the block, using less disk space. The full block size is written to disk as the actual size only when the block contains "real" data.

When reading sparse files, the file system transparently converts metadata representing empty blocks into "real" blocks filled with null bytes at runtime. The application is unaware of this conversion.

Most modern file systems support sparse files, including most Unix variants and NTFS. Apples HFS+ does not provide for sparse files, but in OS X, the virtual file system layer supports storing them in any supported file system, including HFS+. Apple File System APFS, announced in June 2016 at WWDC, also supports them. Sparse files are commonly used for disk images, database snapshots, log files and in scientific applications.

                                     

1. Advantages

The advantage of sparse files is that storage is only allocated when actually needed: disk space is saved, and large files can be created even if there is insufficient free space on the file system. This also reduces the time of the first write as the system doesnt have to allocate blocks for the "skipped" space. If the initial allocation requires writing all zeros to the space, it also keeps the system from having to write over the "skipped" space twice.

For example, a virtual machine image with max size of 100GB that has 2GB of files actually written would require the full 100GB when backed by pre-allocated storage, yet only 2GB on a sparse file. If the file system supports hole punching and the guest operating system issues TRIM commands, deleting files on the guest will accordingly reduce space needed.

                                     

2. Disadvantages

Disadvantages are that sparse files may become fragmented; file system free space reports may be misleading; filling up file systems containing sparse files can have unexpected effects such as disk-full or quota-exceeded errors when merely overwriting an existing portion of a file that happened to have been sparse; and copying a sparse file with a program that does not explicitly support them may copy the entire, uncompressed size of the file, including the zero sections which are not allocated on disk - losing the benefits of the sparse property in the file. Sparse files are also not fully supported by all backup software or applications. However, the VFS implementation sidesteps the prior two disadvantages. Loading executables on 32bit Windows exe or dll which are sparse takes a much longer time, since the file cannot be memory mapped in the limited 4GB address space, and are not cached as there is no codepath for caching 32bit sparse executables Windows on 64bit architectures can map sparse executables. On NTFS sparse file or rather its non-zero areas cant be compressed. NTFS implements sparseness as a special kind of compression so a file may be either sparse or compressed.

                                     

3.1. Sparse files in Unix Creation

The Unix command

will create a file of five mebibytes in size, but with no data stored on disk only metadata. GNU dd has this behavior because it calls ftruncate to set the file size; other implementations may merely create an empty file.

Similarly the truncate command may be used, if available:

On Linux, an existing file can be converted to sparse by:

Alas, theres no portable way to punch holes; the syscall is fallocateFALLOC_FL_PUNCH_HOLE on Linux, fcntlF_FREESP on Solaris.

                                     

3.2. Sparse files in Unix Detection

The -s option of the ls command shows the occupied space in blocks.

Alternatively, the du command prints the occupied space, while ls prints the apparent size. In some non-standard versions of du, the option --block-size=1 prints the occupied space in bytes instead of blocks, so that it can be compared to the ls output:

                                     

3.3. Sparse files in Unix Copying

Normally, the GNU version of cp is good at detecting whether a file is sparse, so

cp sparse-file new-file

creates new-file, which will be sparse. However, GNU cp does have a --sparse option. This is especially useful if a file containing long zero blocks is saved in a non-sparse way i.e. the zero blocks have been written out to disk in full. Disk space can be saved by doing:

cp --sparse=always file1 file1_sparsed

Some cp implementations, like FreeBSDs cp, do not support the --sparse option and will always expand sparse files. A partially viable alternative on those systems is to use rsync with its own --sparse option instead of cp. Unfortunately --sparse cannot be combined with --inplace.

                                     
  • A sparse image is a type of disk image file used on macOS that grows in size as the user adds data to the image, taking up only as much disk space as
  • in numerical analysis, a matrix populated primarily with zeros Sparse file a computer file mostly empty All pages with titles beginning with sparse
  • In numerical analysis and scientific computing, a sparse matrix or sparse array is a matrix in which most of the elements are zero. By contrast, if most
  • The file is not indexed on the host device. Offline O The file data is physically moved to offline storage Remote Storage Sparse P The file is
  • predecessors: sparse file support, disk use quotas, reparse points, distributed link tracking, and file - level encryption called the Encrypting File System EFS
  • using the same format for the file data itself, and relocating the metadata into empty space, in some cases using sparse file support. Migration has the
  • The Harwell - Boeing file format also known as HB format is a file format designed to store information used to describe sparse matrices. Matrix Market
  • sparse disk image a large single file to present a volume for the home directory. Mac OS X Leopard and Mac OS X Snow Leopard use more modern sparse
  • Sparse is a computer software tool designed to find possible coding faults in the Linux kernel. Unlike other such tools, this static analysis tool was
  • Sparse distributed memory SDM is a mathematical model of human long - term memory introduced by Pentti Kanerva in 1988 while he was at NASA Ames Research