5

I created a simple text file. I saw its size. it was 0 bytes. Then i entered alphabet 'a' and checked the size of it. IT says:

Size : 1 bytes

Size on disk: 4.00 KB(4096 bytes)

What is the difference? And why two different values for the same data? OS I am referring to is Windows.

fixer1234
  • 27,064
  • 61
  • 75
  • 116
Sandeep
  • 583
  • 5
  • 10
  • This is quite clearly not a programming question. – unwind May 09 '12 at 09:24
  • 4
    And BTW, if you use disk compression, the size on disk can be smaller than the basic size. – Jerry Coffin May 09 '12 at 09:25
  • sorry but i wanted to read the size of the file programmatically. I was confused as to which size i will be getting. That is where i have got this doubt.. – Sandeep May 09 '12 at 09:26
  • 1
    ... or if you use sparse files – jpalecek May 09 '12 at 09:26
  • **Size on disk is not always greater**. [Small files are stored directly on the MFT and will have size on disk = 0](https://superuser.com/q/1234033/241386). Compressed files often also have smaller size on disk – phuclv Jul 31 '17 at 16:22

1 Answers1

10

The hard drive can be seen as a long string of bits and bytes. The way the hard drive can be used as a thing to hold folders and files is using a filesystem.

Most filesystems use blocks to hold files. Each file consists of one or more blocks. So a file takes at least the size of one block, in your case 4 kilobyte (which is a small block).

This is all highly dependent on which filesystem you use, but this is most likely the explanation.

orlp
  • 1,517
  • 6
  • 17
  • 26
  • Thanks.. But is this not wastage of memory? – Sandeep May 09 '12 at 09:30
  • @happy2Help: yes it is, but it brings other benefits which are much more important: speed mostly. It's a tradeoff. – orlp May 09 '12 at 09:32
  • hmm interesting.. This must be a common problem, if there is a name to this, kindly state it. will find more on google about this.. Nice Answer.. +1 for keeping it short and understandable.. thanks – Sandeep May 09 '12 at 09:37
  • 2
    @happy2Help The process that allocates files to blocks is called "blocking". The space wasted by allocating only whole, unused blocks to a file and probably not filling the last one completely is called "slack space". Some modern filesystems attempt to overcome this with a process called "block suballocation" which, as the name implies, allows a block to contain parts of multiple files. – Indrek May 09 '12 at 09:53
  • If it is fat or ntfs file system, fragmentation can add to "size on disk" – Moab May 09 '12 at 14:08
  • @Moab that is wrong. Even if a file is 100% fragmented, i.e. it doesn't consist of any contiguous blocks, still only the last block will be partially filled. – Ro-ee Sep 20 '16 at 23:19