Why can’t I create a file equal to the available disk space?

Raymond Chen


A customer was trying to exercise some boundary cases in their application, one of which entailed “out of disk space” scenarios. The way they did this was very simple: Call Get­Disk­Free­Space­Ex to see how much disk space is available, and then create a file of that size. This worked on some volumes, but on other volumes, it failed with “Not enough disk space.” Even after making sure no other programs were accessing the drive in question, the error persisted. Why?

The amount of disk space required to create a file of a particular size is not exactly equal to the number of bytes in the file. After all, the file system also needs to remember the file name, the security attributes, various timestamps, and information to keep track of where on the disk all the bytes of the file are stored. And then there are the less obvious things like the change journal records to record that the file was created and object tracking records for link tracking.

Just because your volume has exactly X bytes free doesn’t mean that you will be able to create a file whose size is exactly X, because additional disk space will be needed to keep track of the metadata. If you want to fill a disk to the brim, you could start by filling it nearly to the brim, then growing the file until you get “Not enough disk space.” If you want to get fancy, you could grow the file by large chunks until you can’t add large chunks any more, and then switch to smaller and smaller chunks. You may also want to use the Set­File­Valid­Data function to avoid wasting time writing zeroes to all the sectors.


Comments are closed.