Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OutofMemoryException was Unhandled

Posted on 2009-04-01
2
Medium Priority
?
727 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
[X]
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
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 500 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

661 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