A StreamReader will use byteBuffer.Length memory between calls. If you did not specify a default value, it uses 1024 bytes. It also allocates a char[] charBuffer size encoding.GetMaxCharCount(bufferSize); which allocates two bytes per element in the array.
If you do not pass the Stream object and let it generate its own FileStream , it will use the filter buffer to store the default 4096 .
The ReadLine call itself will allocate a StringBuilder internally and will read the data in byteBuffer , then decode the bytes and store them in a charBuffer , it will then copy the characters from the charBuffer and into StringBuilder , which will then be returned to you via a .ToString() call.
So, in the general case, new StreamReader(@"C:\Users\UK\Panfile.txt") alone it will allocate 1024 + (1025 * 2) + 4096 1 bytes of memory (5120 in total), and during the ReadLine call it will allocate no more than line.Length * 2 + StringBuilderOverhead + line.Length * 2 2 . *2 you see for char[] , because each char takes up two bytes.
1: byteBuffer + charBuffer + FileStream buffer
2: char[] inside StringBuilder + any free space in the string strobe buffer + string returned by the call to .ToString() .
source share