4

I have application which write to file every minute. Sometimes (every 10 minutes +-) I get error

There is not enough space on the disk

My app is a Windows Forms application. I read a lot articles on google but it didn't give me any result how to fix it.

Exception:

Exception thrown: 'System.IO.IOException' in mscorlib.dll

My code:

try
{
    FileStream stream = new FileStream(file, FileMode.CreateNew);
    FileStream stream2 = new FileStream(file2, FileMode.CreateNew);
    BinaryFormatter writer = new BinaryFormatter();

    writer.Serialize(stream, GetProducts().Take(80000).ToList());
    writer.Serialize(stream2, GetProducts().Skip(80000).ToList());
    stream.Flush();
    stream.Close();
    stream2.Flush();
    stream2.Close();
}
catch(Exception ex)
{
    Debug.WriteLine($"FAIL to write: {i} - {ex.Message}");
}

My total free space on disk is 74GB. Before last run of program I did defragmentation.

How am I supposed to get rid of this error?

Thanks

EDIT: Screen available here

EDIT2: Stacktrace

     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
     at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
     at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
     at System.IO.FileStream.Dispose(Boolean disposing)
     at System.IO.FileStream.Finalize()

link to another screen

bolov
  • 72,283
  • 15
  • 145
  • 224
user1085907
  • 1,009
  • 2
  • 16
  • 40
  • 1
    How much are you trying to write? – Patrick Hofman Mar 16 '17 at 08:59
  • To what location are you trying to write? – Patrick Hofman Mar 16 '17 at 09:00
  • When error started it was probably around 100k products (40MB). Now I started from zero and still happens.. wtf.. 250 products and I'm getting error (50kb probably) – user1085907 Mar 16 '17 at 09:01
  • Patrick: location is my data disk (not system), full access – user1085907 Mar 16 '17 at 09:02
  • What folder exactly? – Patrick Hofman Mar 16 '17 at 09:02
  • 1
    You can try to implement *retry*-strategy if you think it's something what shouldn't happens. And don't handle all exceptions, there is a way to determine [when there is no more space](http://stackoverflow.com/q/9293227/1997232). – Sinatr Mar 16 '17 at 09:03
  • F:\Work\Programming\Project\Project\bin\Debug\AS – user1085907 Mar 16 '17 at 09:03
  • Sinatr: what is strange is that my VS 17 shows me that exception happens inside .NET framework which sounds like a bug. Even if I have implemented try catch my program shuts down – user1085907 Mar 16 '17 at 09:06
  • 2
    Can you show stack trace? – Sinatr Mar 16 '17 at 09:07
  • Sure, just I have to wait to get that exception again, I didn't log it :/ – user1085907 Mar 16 '17 at 09:07
  • Small aside note, you should have `using(...){...}` blocks around those streams. And always log your errors. – H H Mar 16 '17 at 09:09
  • @user1085907 To confirm, the folder isn't a drive mounted to the OS, or a symbolic link to another drive, is it? – ProgrammingLlama Mar 16 '17 at 09:09
  • 1
    Also, how much room (quota) do you have in your Temp folder? I'm not sure how the BinFormatter works but some of its kin will produce temp files. – H H Mar 16 '17 at 09:13
  • john: is completely separated disk, its not mounted to OS – user1085907 Mar 16 '17 at 09:18
  • Henk: I do not have any quota for Temp folder. OS Drive has 75GB left. – user1085907 Mar 16 '17 at 09:19
  • Then we wait for the stack-trace. – H H Mar 16 '17 at 11:02
  • I'm running that program for few hours and still can't get that error now, I did not modify anything.. wtf. When I need it to crash I can't.. When I need it to work it keeps crashing.. As soon as I get stack trace I will post it here – user1085907 Mar 16 '17 at 15:38
  • Screen available in main post – user1085907 Mar 16 '17 at 15:50
  • A missed opportunity. We need the stack trace (not the call stack window), that's what that _View Details_ link is for. Also, post it as text (copy/paste it), not as a picture. – H H Mar 16 '17 at 21:38
  • Well another screen, cause there is nothing to copy :/ https://ctrlv.cz/shots/2017/03/16/KVPP.png – user1085907 Mar 16 '17 at 22:46
  • Disabled in tools Just my code to get stacktrace :/ Didnt know that VS 17 enables it by default :( Updated main post – user1085907 Mar 16 '17 at 22:55
  • Interesting thing is the Finalize() on the top of the stack... Not sure if this can result from Close(). Could there be another file, in another thread perhaps? – H H Mar 17 '17 at 11:17
  • Looking at this again, it's clear the error can't be from the code you posted here. The stack points to a Finalizer (destructor) calling `Dispose(false)`. So there must be a leaked (abandoned) file somewhere, inside GetProducts() or on another thread. But even then it's a strange error, look for fixed MemoryStreams or NetworkStreams and such. – H H Mar 20 '17 at 08:13
  • GetProducts returns List of Product class, there is no usage of file inside method – user1085907 Mar 20 '17 at 09:20
  • Well, the error is not what or where you think it is... You'll have to search wider. Do apply the advise about `using()` blocks. And it seems you only need 1 stream at a time, save some memory. – H H Mar 20 '17 at 10:20

1 Answers1

1

What is interesting from your exception stack, is that the error happens when you call the "Close" method, and it calls "Flush" internally. However, you already have successfully called "Flush" explicitly on the previous line in code. So I would expect the storage exception to be thrown on the explicit "Flush()" call. That raises doubts regarding the actual error reason. What I would try to do: 1. Wrap the disposables in "using" blocks 2. Don't call "Flush()" explicitly, as the method will be called anyway during Dispose/Close.

If that would still fail, try to log the current free space of the drive you try to write to right before writing data. The following method will help you with that:

    private static long GetAvailableSpace(string path)
    {
        string drive = Path.GetPathRoot(Path.GetFullPath(path));
        DriveInfo driveInfo = new DriveInfo(drive);
        return driveInfo.AvailableFreeSpace;
    }

Hope this helps.

Artak
  • 2,819
  • 20
  • 31