Performance and Memory: StreamWriter/StreamReader/FileStream...

Alrighty, this is going to be a short. First, I'd like to point out the default memory requirements for each of these constructs.

  • StreamWriter/StreamReader - character array /* 2K bytes */, byte buffer to hold chars /* 1K or 2K bytes */, FileStream if in use /* 4K */
  • FileStream - input-output buffer /* 4K bytes */

Those are the defaults. Now, that puts the StreamWriter and StreamReader taking twice the memory footprint in some cases over the basic FileStream. You really only need the StreamWriter and StreamReader if you are doing textual processing, so if you aren't then you might want to leave them out.  All of these guys have parameters to customize the size of buffers.

  • StreamWriter/StreamReader - min size is 128 characters or /* 256 bytes + 128 or 256 bytes for byte array */, 4K file stream if you don't use the overloads that take a Stream by default so you can pass in a better FileStream.
  • FileStream - Apparently there is no minimum size.

That leads me to some tips.

  • StreamWriter/StreamReader - Tune your buffer sizes and see if you can't eek extra performance out based on your data. There are so many optimizations buried in this code with regards to specific scenarios (namely scenarios where a user buffer is used for decoding rather than the internal buffer), that each scenario needs to be examined.  Don't play with the FileStream unless you are trying to extend the user buffer optimization to the file IO layer as well. Doing this can be potentially dangerous.
  • FileStream - Use this in place of the stream encoding classes if you can. Users often make use of the wrong stream type when working with things. For instance, users will load binary readers and writers when copying data from network streams down to files. This extra level of indirection is unnecessary.
  • FileStream - Make use of the user buffer optimization. If the user buffer is the same size or larger than the buffer allocated internally, then the user buffer is used in place of the internal buffer. This removes a memory copy. This is most readily apparent when using the Read/Write methods that take a byte array. The internal buffer is delay allocated, so I think under some circumstances you can prevent it from ever being allocated even though you set an internal buffer size when creating the FileStream.

Honestly, the file IO stuff is one area that I've neglected because the performance is always suitable. Some new methods that I'm working on require much higher performance bars so hopefully I'll get a chance to look at these classes in more detail and provide some source code demonstrating and even testing various performance characteristics. At the end of the day, the MS guys have tuned their classes to work best in the general circumstances, so only in edge cases should you consider trying to optimize for better performance. The question of the day would be, “What are the edge cases?”, and hopefully I can start to answer that as I find them.

Published Saturday, July 10, 2004 2:40 AM by Justin Rogers

Comments

Monday, July 12, 2004 3:10 AM by Jonathan de Halleux

# re: Performance and Memory: StreamWriter/StreamReader/FileStream...

You should provide some benchmark if you talk about performance :)

You can use my benmarch framework NPerf to do that easily ( http://www.codeproject.com/gen/design/nperf.asp )

Cheers,
Jonathan
Monday, July 12, 2004 4:32 AM by Justin Rogers

# re: Performance and Memory: StreamWriter/StreamReader/FileStream...

Hey, no bullshit advertising ;-) Eck, just kidding. The above are general guidelines. Demonstrating performance on disk bound IO is going to be different depending on many circumstances.

Generally I make a real habit of providing performance benchmarks, but when the cold start time of a Windows Forms application can range between 5 the 25 seconds on the same machine (all disk bound IO related), then you can see where local benchmarks don't really mean much.
Monday, July 26, 2004 2:56 PM by TrackBack

# S dot One heeft het over .NET » Optimalisatie van StreamWriter/StreamReader/FileStream

S dot One heeft het over .NET » Optimalisatie van StreamWriter/StreamReader/FileStream
Saturday, October 04, 2008 7:03 AM by ویدیو کنفرانس

# re: Performance and Memory: StreamWriter/StreamReader/FileStream...

i have a flash file embeded in my app and want to write it to a physical place so i can set the flash component movie property and play it.the problem is that i can access the file as StreamReader and i want to save it elsewhere so i use the StreamWriterbut the file content is getting corrupt.i want to have a sample to copy a file with StreamReader and StreamWriter.

tnx

Wednesday, September 16, 2009 4:14 PM by David

# re: Performance and Memory: StreamWriter/StreamReader/FileStream...

Actually, the FileStream has a minimum buffer size of 8 bytes, but if you use a parameter less than 8 it will invisibly round it up to 8.

Playing with buffers is a tricky business, and generally I avoid it altogether since it generally proves self-defeating.  The drive is buffering sectors, the drive controller is buffering the drive, the OS is buffering the drive controller, the app buffers the OS...it gets a little redundant after awhile and the land of diminishing returns is generally right underneath your feet.  I will, however, be interested to see what your results prove to be.

Thursday, March 25, 2010 7:56 AM by Gannon

# re: Performance and Memory: StreamWriter/StreamReader/FileStream...

Good afternoon. Is there life before death? Help me! Looking for sites on: Generic flonase price. I found only this - <a href="genericflonase.info/">flonase versus nasonex</a>. Flonase, those ipods then think you really psychic. Almost place liquid as minutes, antibiotics and emergency can be enjoyed and viewed, flonase. With best wishes :o, Gannon from Uganda.

Leave a Comment

(required) 
(required) 
(optional)
(required)