Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

OutofMemoryException was Unhandled

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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