I am getting out of memory alert in this function, called 500K times:
public void DoStuff(string msg)
{
rtfTerminal.Invoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;
rtfTerminal.AppendText(msg);
rtfTerminal.ScrollToCaret();
}));
}
In the global scope I declared:
private System.Windows.Forms.RichTextBox rtfTerminal;
- I guess I get the out of memory since I do
new EventHandlereach
call, How can I avoid that? - Is it reasonable that I get the out of memory due to
rtfTerminal.AppendText(msg);?
The
new EventHandleris not a massive problem, nor is the anonymous object that you can’t really see (but which exists): both of these will be short-lived, i.e. GEN-0 and will virtually free to collect. Additionally, GC will kick in if things start to run low, so that shouldn’t be a problem. Continually adding text to aRichTextBoxwithout limit could be a problem. I would try to avoid that. Files are usually a better option for dumping output from long-running processes.Ultimately, if that doesn’t help: you need to use something like a memory profiler to see where the memory is going.