1

I'm working on an application that should store some PDF for later printing. I have an implementation that is checking what size the PDF is and according to the size I choose between Memory or File Streams (<2GB = MemStream and >2GB = FileStream).

When I am using the FileStream the performance varies and I just noticed that this is caused by the Windows Memory Cache. Basically everything i am using to write/read to/from a FileStream will get into this cache and after saving 6 PDFs I get really low performance (30 seconds compared to 60s+)

I declare my FileStream in the following way:

FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 524288, FileOptions.WriteThrough))

I just added the WriteThrough option but it does not seem to fix the issue. Is there a possibility that after I save the file to disk and close/dispose the FileStream I get the cache memory cleared?

Thank you in advance!

__________edit___________

adding code snippets.

            loadStream = new FileStream(@"C:\temp\FileStream_test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 524288);
            request.InputStream.CopyTo(loadStream);
            loadStream.Flush();
            loadStream.Position = 0;

and 2nd one

        using (var mimePDL = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 524288, FileOptions.WriteThrough))
        {
            mimeParts[contentID].Content.DecodeTo(mimePDL);
            mimePDL.Position = 0;
            mimePDL.Flush(true);
            mimePDL.Close();
            mimePDL.Dispose();
        }

And for the first one I dispose of it this way

            loadStream.Close();
            loadStream.Dispose();
            File.Delete(((FileStream)loadStream).Name);
nCis
  • 68
  • 10
  • Call the garbage collector: GC.Collect(); – LordPupazz May 26 '20 at 13:40
  • @LordPupazz is it safe to do so? – nCis May 26 '20 at 13:42
  • 1
    Also consider enclosing you `FileStream` in a `using` statement to ensure it is disposed once out of scope. For example: `using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 524288, FileOptions.WriteThrough)) { // Do you process here }` – George Kerwood May 26 '20 at 13:42
  • @GeorgeKerwood I am doing this for one FileStream. I am actually using 2, and one of them can't be enclosed inside a using statement, but both seem to do the same thing. – nCis May 26 '20 at 13:44
  • There seem to be no benefit to using a larger buffer size than 128kb. See [optimal buffer size](https://stackoverflow.com/questions/1862982/c-sharp-filestream-optimal-buffer-size-for-writing-large-files) – JonasH May 26 '20 at 13:45
  • @40love Yes, if you've finished with the object. Also as George Kerwood says, enclose the FileStream in a using bracket or call the Dispose() method on it after you're done. – LordPupazz May 26 '20 at 13:45
  • How about the object (PDF) that you are writing to file? Is it disposed after the save? – George Kerwood May 26 '20 at 13:47
  • @GeorgeKerwood the object comes from a MIME submission, that request which contains everything is disposed. – nCis May 26 '20 at 13:50
  • @40love Then I'm afraid I'm out of ideas, other than to perhaps confirm that your last comment is. Do not assume that the GC has disposed of everything just because they are no longer in scope, it can choose when and why in it's own mysterious ways. (Apologies if you knew that already) Good Luck – George Kerwood May 26 '20 at 13:54
  • @GeorgeKerwood no need for apologies, any tip is helpful :) – nCis May 26 '20 at 13:59

1 Answers1

0

In my opinion you shouldn't store PDFs in memory if you aren't manipulating them.

After manipulations, just save the file and dispose the stream.

If you need some metadata, just take them in your application instead of the entire file.

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • Actually the PDF comes inside a MIME file that is submitted to me. For this MIME file I process each part of it and for the PDF part I just save it to disk, but before saving it i have to parse the whole submitted content and this content is saved into a filestream aswell if it is >2GB. – nCis May 26 '20 at 13:49