Why does the space occupied by a file exceed the actual file size?
Published: 2024-06-04
Taking the Windows system as an example, first look at a few images:
Figure 1: The file size is smaller than the occupied space.

Figure 2: The file size is the same as the occupied space size.

Figure 3: The file size is larger than the occupied space size.

The three images above show several situations regarding file size and occupied space size. The first two scenarios are common. The reason why sometimes the file size is smaller than the occupied space is due to cluster size limitations. A cluster represents the minimum amount of disk space that can be allocated to save a file. The cluster size of the area where the sparse.txt file is stored is 4096 bytes. In Figure 1, the file size is 4096 bytes (you can use the chkdsk command to check), so a 5K file needs 2 clusters to store, as shown in the following image:

In Figure 2, the file size is 5M. 5M is exactly an integer multiple of 4096, so the file size is the same as the occupied space size.
The following focuses on the third situation, which involves sparse files. This situation does not occur on systems that do not support sparse files. Here is an example:
/**
* Conventional files may waste a lot of unnecessary space
* @throws IOException
*/
@Test
public void normalFile() throws IOException {
File file = new File("d:/tmp/sparse.txt");
Path path = file.toPath();
FileChannel ch = FileChannel.open(path, StandardOpenOption.CREATE,StandardOpenOption.WRITE);
ch.position(5*1024*1024L);
ch.write(ByteBuffer.wrap("hello world".getBytes("utf-8")));
ch.close();
}
/**
* Sparse file mode
* @throws IOException
*/
@Test
public void sparseFile() throws IOException {
File file = new File("d:/tmp/sparse.txt");
Path path = file.toPath();
FileChannel ch = FileChannel.open(path, StandardOpenOption.SPARSE,StandardOpenOption.CREATE_NEW,StandardOpenOption.WRITE);
ch.position(5*1024*1024L);
ch.write(ByteBuffer.wrap("hello world".getBytes("utf-8")));
ch.close();
}
Some users may find that the file size counted in file management is inconsistent with the actual space occupied,for example, when the space size indicates over-limit while the file size does not indicate over-limit,which is also caused by these reasons. In summary, the occupied space size detected by our system is the actual space occupied by the file, not the size of the file itself. If the system prompts that space is over-limit, please upgrade your space size to avoid affecting your normal use.
If your hosting quota is significantly over-limit but the actual occupied space is not large, it is because there are many small cache files, such as files under the \runtime\session path, as shown below:

If deleting these files via FTP or file manager is too slow, please【submit a ticket】, and you may authorize technical engineers to batch delete them for you.
