Solved

OutofMemoryException was Unhandled

Posted on 2009-04-01
2
698 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

23 Experts available now in Live!

Get 1:1 Help Now