I receive around 5 messages per second. Each of them has a string, which I concatenate to a master string that contains all the received messages
string _masterText = "";
public void AddNewMessage(string text) // this is going to be call at least 5 times/second
{
_masterText += text;
}
Is this the appropiated way to do it? or should I use instead StringBuilder, like:
StringBuilder _masterText = new StringBuilder();
public void AddNewMessage(string text) // this is going to be call at least 5 times/second
{
_masterText.Append(text);
}
Thanks
StringBuilder is mutable. StringBuilder performs faster than string when appending multiple string values. Use StringBuilder when you need to append more than three or four strings. Use the Append () method to add or append strings to the StringBuilder object.
You can create an object of the StringBuilder class using the new keyword and passing an initial string. The following example demonstrates creating StringBuilder objects. using System.Text; // include at the top StringBuilder sb = new StringBuilder(); //string will be appended later //or StringBuilder sb = new StringBuilder("Hello World!");
Although the StringBuilder class generally offers better performance than the String class, you should not automatically replace String with StringBuilder whenever you want to manipulate strings.
Hello C#. Use the AppendFormat () method to format an input string into the specified format and append it. Use the Insert () method inserts a string at the specified index in the StringBuilder object. StringBuilder sb = new StringBuilder("Hello World!"); sb.Insert (5," C#"); Console.WriteLine (sb); //output: Hello C# World!
StringBuilder is generally considered the better option, but in this case I'd use neither.
Even with StringBuilder, at that rate the underlying character buffer will soon itself be large enough to get stuck on the Large Object Heap. This will cause problems for the health of an application that needs to stay running for a while.
Instead, I'd use a System.Collections.Generic.List<string>
and just call it's .Add()
method for each new message. Depending on what you do with these messages you may also find another collection type is more appropriate (perhaps a Queue<string>
), but this is the direction you should go. By using a collection, the memory used by each individual string will not count towards the size of the collection object. Instead, each string will only add a few bytes for the reference. This will take a lot longer to run into problems with compacting the Large Object Heap.
If you still have problems after switching to a collection, you can use a stream, and write the strings to disk. This way, you never have more than one string in RAM at a time. Now, the only way you have problems is if individual strings are 85000 bytes or larger.
Remember that the String
class is immutable. It is not possible to change a string. When you are "concatenating" strings, you are really creating a new string, and copying the contents of the original string to it, then adding the contents of your new string.
This starts to use memory very quickly if you're appending large strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With