Every streamReader.ReadLine()
will always create a new string which will be garbage collected but until GC it will exist in memory. Your memory consumption can drop cause String.Intern
returns the system's reference to string, if it is interned; otherwise, a new reference to a string with the value of string and your list
will consist from references to the same instance of string which was interned making the ones created by streamReader.ReadLine()
available for GC.
var str = "Test"; // compile time constant will be interned by default
var str1 = new string(str.ToArray()); // simulate reading string
Console.WriteLine(object.ReferenceEquals(str1, string.Intern(str1))); // prints false
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…