How much memory affects StreamReader

When I use StreamReader, as shown below, how much memory is affected. I assume that since each line is read into the variable "line", only this line remains in memory, and overall this is good. My goal is not to burden memory too much when I read a very large amount containing thousands of lines. Clarification will be appreciated.

string line = string.Empty; using(StreamReader Reader = new StreamReader(@"C:\Users\UK\Panfile.txt")) { while((line = Reader.ReadLine())!= null) { //other code to process the line now being read. } Reader.Close(); Reader.Dispose(); } 
+5
source share
2 answers

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() .

+8
source

It will only store one line in memory at a time, so this method is ideal for processing large files without consuming a lot of memory.

+2
source

Source: https://habr.com/ru/post/1257418/


All Articles