3

I am writing a service that saves files and may potentially fill up one storage and may have a secondary storage to fall back to. I'd like to monitor the amount of free space on the primary storage and quit even trying to write to it if it's running low on free space. To do this, I was thinking of calling GetDiskFreeSpaceEx somewhat frequently - maybe once every few seconds. Is GetDiskFreeSpaceEx expensive - is there a reason not to do this? OR, is there a better pattern to accomplish what I'm trying to do?

[EDIT] The kind of answer I was hoping for might come from either

A)someone who has an idea of what GetDiskFreeSpaceEx actually does behind the scenes who might say either "don't do that, the system does all the expensive operations when you ask for the free disk space" or "that's fine to do; the system keeps a count of that in memory and just has to retrieve it for you" or

B)someone who has written an application with a similar requirement, and already been down this road and down the research

Aerik
  • 2,307
  • 1
  • 27
  • 39
  • Have you ran a benchmark to see how "expensive" this is? – Ron Beyer Nov 23 '15 at 19:39
  • Possible duplicate of [How can I determine if a remote drive has enough space to write a file using C#?](http://stackoverflow.com/questions/137255/how-can-i-determine-if-a-remote-drive-has-enough-space-to-write-a-file-using-c) – MethodMan Nov 23 '15 at 19:42
  • No, if I benchmark it I will have answered my own question (which I will do if no one else has an answer). I don't think it's a duplicate, as I know *how* to do it, I just want to know how expensive it is or if there's a better pattern for watching free disk space – Aerik Nov 23 '15 at 19:47
  • This is the kind of thing where you benchmark it on your machine, and it works fine, then it turns out that some other edition of Windows has different performance characteristics than your desktop. :-( – Moby Disk Nov 23 '15 at 20:02
  • FWIW, I seem to recall that it's usually cheaper to attempt to write out a dummy chunk (64K or whatever) and then truncate the file again if it succeeds. Of course if it failed you ran out of space. – 500 - Internal Server Error Nov 23 '15 at 20:05
  • @Aerik [Read this from Eric Lippert](http://ericlippert.com/2012/12/17/performance-rant/). – Ron Beyer Nov 23 '15 at 20:07
  • @RonBeyer - that's interesting, but I think not quite what I'm asking. I'm not trying to ask what's faster, what I'm asking is more along the lines of "I'm thinking of doing this... is it stupid? Has someone accomplished the same objective using a different technique" – Aerik Nov 23 '15 at 20:32
  • Do it only when you already are accessing the disk for other reasons. (Preferably when writing, since that already needs to know available disk space.) That way you aren't spinning up the disk unnecessarily. – Raymond Chen Nov 23 '15 at 21:01

0 Answers0