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 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 aQueue<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.