What kind of algorithm do they use to see how much space is left? I thought that maybe the amount of space it began with is programmed in and then the size of files created is then subtracted from the total amount.

P.S: This is a Mac question or a programming question?

  • Do you mean hard drive space or volume free space left? – klanomath Aug 23 '15 at 16:36
  • How much space is left all together – Steampunkery Aug 23 '15 at 16:41
  • 1
    A fully formatted hard drive with an empty hfs+ volume has 0 blocks left and at the same time myriads of bytes (minus some dozen megabytes for the catalog etc.) of free space. – klanomath Aug 23 '15 at 16:48

Your question is hard to answer because you don't mention the layer you are looking at.

There at least four layers to consider:

  • Hardware layer

    Usually the hardware consists of some administrative blocks, the "payload" blocks and some spare blocks. A hard drive contains some dozen and an SSD drive up to 15% of the official size of spare/reserve blocks. Bad blocks are remapped to the spare/reserve blocks by the drive's firmware. The operating system only knows the size of the payload blocks. Some low-level disk tools (e.g. Spinrite) may read out the number of available spare blocks.

  • Partiton layer

    Depending on the size of the hard disk - propagated by the firmware/hd-controller to your EFI/operating system - your partitioning tool (e.g. Disk Utility) may create partitions by determining start values and end values for each partition in the partition table. The free space on your hard disk is determined by the final block of the last partition and the beginning of the second GPT table (provided that you partitioned your drive continuously and gaps don't exist). You may read out the free space on your hard disk with gpt.

  • Volume layer

    Usually partitions have to be formatted with a file system to be usable. Almost all important filesystems contain a volume bitmap. The volume bitmap is a representation of all (file system) blocks or clusters of a volume. Free blocks and occupied blocks are marked differently. So one method to determine the free space on a volume is counting all blocks tagged free in the volume bitmap.

  • LVM layer (Logical Volume Manager) or CoreStorage

    The LVM layer is an abstraction layer in between hard disks, partitions and volumes. Though determined by the basic partitions ("Physical Volumes") a Logical Volume may span over several Physical Volumes (Fusion Drive). I don't know it for sure, but a Logical Volume Group probably contains a counterpart of a partition table holding all informations about all contained objects (like Physical Volume(s), Logical Volume Family(ies), Logical Volume(s)). The available free space may be read out by entering diskutil cs list

  • block/cluster layer (this is not relevant for OS X file systems)

    Some file systems support block suballocation. In short words: A lot of files don't occupy a full block/cluster of the volume layer because they are smaller than the block/cluster size. In the free space of the cluster you may store a second/third/forth small file. I don't know how to determine this free space (probably some i-node/B-Tree magic).

Depending on the layer you may now choose the appropriate method or tool to determine the "free space of your hard disk".

  • nice answer! totally worth reading! – AlessioX Aug 23 '15 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .