Solved

OutofMemoryException was Unhandled

Posted on 2009-04-01
2
701 Views
Last Modified: 2013-12-17
I am developing a multi-threaded Windows application using Visual Studio 2008 and Visual C#. One of the threads calls the function below. This function updates the user with the progress of the application by writing a log to a text-box.

I was running the application today and got an OutofMemoryException unhandled error at
 " this.Invoke(writer, new object[] { line });"

I obviously need to catch this error using try-catch block but I need to know what is causing this error and how to handle it properly. Is there anything obviously wrong with the code?


      public void writeToConsole(string line) {

            if (textBox1.InvokeRequired) {
                ConsoleWriterDelegate writer = new ConsoleWriterDelegate(writeToConsole);
                this.Invoke(writer, new object[] { line });
            }
            else {

                string toAdd = DateTime.Now.ToString() + ": " + line;

                textBox1.Text += toAdd;
                textBox1.Text += Environment.NewLine;

                textBox1.Select(textBox1.Text.Length - 1, 0);
                textBox1.ScrollToCaret();
            }
        }
0
Comment
Question by:rangers99
2 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24039614
Do you have any loops that could be calling this? chances are you could have an infinite loop that calls this method
0
 
LVL 3

Accepted Solution

by:
MikeDotNet555 earned 125 total points
ID: 24041621
What is the lenght of the textbox text when you get that error?

Also even if an OutOfMemory error occurs on that line, it does not necessarily mean that the OutOfMemory problem is due to that specific line; you could have a memory "leak" somewhere else that would prevent the textbox from appending text.

I would also look into using RichTextBox instead of TextBox, I do not think a TextBox was intended to hold alot of text, and appending a new string to an existing string requires to make a copy of the initial string value, a copy of the appended string and another copy of the initial + appended text, that virtually double the size of your string. And I am pretty sure that a rich text box deal with multi line better (it probably use an internal string array optimized for multiline, and you can call AppendLine).

There are other things I would be looking for, namely:
-Registered events that does not get unregistered when you are done with them, that will cause the .NET runtime to keep references to the event object so the GC will never collect it.
-If you use sockets with XXXAsync methods, be sure to dispose the SocketAsyncEventArgs when you are done with them or they will never get deleted because .NET keep a reference to them internally.
-If you use ALOT of threads, you might think about alternative ways, namely overlapped or the use of threadpool; each created thread use about 2-3mb of stack space, modifying the stack size may help but it is higly not recommended by microsoft documentation.

Finally, if after all this you can't find the problem, there are apps that can profile memory so you can see live all objects created and how much space they use, space used by child, the references to and from all your objects etc. That can of program can be really usefull. You can use spices.net but it is a 14day trial and a license cost alot of money. There is also a free program that does the same thing but while being really powerfull, it is alot harder to use. I dont remember the name of that program but if you need it after tryied the above things, let me know I will try to find it back.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now