-2

When we look at the size of a file in any file management software such as the Windows Explorer, we see two different sizes, one that reads just Size and the other reads Size on disk.

enter image description here

  1. Why are these two different?

    Does the size on the disk include some overhead such as its entry in the File Allocation Table (FAT)? Could you please elaborate?

  2. When we programmatically query the size of a file, it always returns the size independent of the disk size. Where does it get this number from? Does the OS have to read the contents of the entire file to determine this size or just look up the file allocation table?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 4
    related: https://superuser.com/q/66825/302 – Rowland Shaw May 11 '17 at 07:38
  • 3
    Even though your question contains code, it's core nature is on the topic of computers instead of programming. As such, it would be better suited to [SuperUser](https://superuser.com/) instead. – Abion47 May 11 '17 at 07:39
  • Thank you. I have read the answer on superuser and it satisfies point (1) of my curiosity. Could you please leave this question as is so someone may answer part (2)? – Water Cooler v2 May 11 '17 at 07:44
  • Far better would be for you to ask one question only. Edit the question to ask just question 2. – David Heffernan May 11 '17 at 07:49
  • 1
    For part 2, the logical file size and several other *attributes* are stored separately from the file in the directory table of the file system. – zett42 May 11 '17 at 08:00
  • 1
    The size is the actual amount of data the file takes up, whereas the size on disk is the amount of space allotted for that file on the drive. Like I said, the crux of this question is not programming-related, so it is off-topic for SO. – Abion47 May 11 '17 at 08:03
  • @zett2 Well, that depends on the file system. It's not always the way you describe it. It does not have to be. – David Heffernan May 11 '17 at 08:10
  • See [How does Explorer calculate “Size on disk”?](https://blogs.msdn.microsoft.com/oldnewthing/20160427-00/?p=93365), and also [Why can’t Explorer decide what size a file is?](https://blogs.msdn.microsoft.com/oldnewthing/20110315-00/?p=11223). – Remy Lebeau May 11 '17 at 15:46

1 Answers1

4

Found this on Super User. What he says:

We know that a disk is made up of Tracks and Sectors. In Windows that means the OS allocates space for files in "clusters" or "allocation units".

The size of a cluster can vary, but typical ranges are from 512 bytes to 32K or more. For example, on my C:\ drive, the allocation unit is 4096 bytes. This means that Windows will allocate 4096 bytes for any file or portion of a file that is from 1 to 4096 bytes in length.

If I have a file that is 17KB (kilo bytes), then the Size on disk would be 20.48 KB (or 20480 bytes). The calculation would be 4096 (1 allocation unit) x 5 = 20480 bytes. It takes 5 allocation units to hold a 17KB file.

Another example would be if I have a file that is 2000 bytes in size. The file size on disk would be 4096 bytes. The reason is, because even though the entire file can fit inside one allocation unit, it still takes up 4096 of space (one allocation unit) on disk (only one file can use an allocation unit and cannot be shared with other files).

So the size on disk is the space of all those sectors in which the file is saved. That means,usually, the size on disk is always greater than the actual size.

So the actual size of a file(s) or folder(s) should always be taken from the Size value when viewing the properties window.

Community
  • 1
  • 1
Priyank
  • 1,513
  • 1
  • 18
  • 36