2

I know that I have a memory leak. I am writing in C# using Winforms. Basically, my program has a lot of buttons. Every .01 seconds, the timer classes calls a function to take in gps data from a gps. Each time a button is pressed, it gets paired with the most recent gps point and they both get pushed to a database. I don't know where the leak is. Could it be with the event handlers? Each button has a += but never a -=. Or could it be with when I post the gps point to the gui? I am writing to the database with entity framework.

Thanks,

Jessi

EDIT: I know it is a memory leak because when I go into task manager I see memory being used by my program going consistently up. Also, the timer doesn't fire as often the later it gets...goes from .01 seconds to about .5 seconds. In the auto generated code, each button is subscribed to (last line): this.commentsGoButton.Location = new System.Drawing.Point(348, 23); this.commentsGoButton.Name = "commentsGoButton"; this.commentsGoButton.Size = new System.Drawing.Size(67, 70); this.commentsGoButton.TabIndex = 12; this.commentsGoButton.Text = "Enter Comment"; this.commentsGoButton.UseVisualStyleBackColor = true; this.commentsGoButton.Click += new System.EventHandler(this.commentsGoButton_Click);

Also, I am calling this function:

        GPS gps1 = new GPS(Lat, Longi, Alt, Yaw, Pit, Rol); 
        info.takeInGPS(gps1);
        str = Lat + "   " + Longi + "   " + Alt + "   " + Yaw + "   " + Pit + "   " + Rol + "   ";
        gui.addToTextBox(str);

Where GPS is the class I am calling it from. GPS is the class that takes in the point from the GPS. It is called by the timer inside the GUI class. GPS passes it to info to pass to the database and the the GUI class to post it using:

    public void addToTextBox(string s)
    {
        textBox.Text += s += "\r\n";
        textBox.SelectionStart = textBox.TextLength; //scrolling stuff
        textBox.ScrollToCaret();
    }

Info does this:

public void takeInGPS(GPS g) { gps = g; write(gps); }

What 3rd party programs will point out WHERE the memory leak is? Could the fact that I am creating 100 new GPS objects a second be the issue? Aren't I overwriting them?

  • 4
    Yes, it could be anything and anywhere. You could check this by debugging. I can not provide you more information with no code. – Fabian Bigler May 24 '13 at 21:51
  • 1
    Some sample code of where you think the issue might be could help. – Dave Zych May 24 '13 at 21:53
  • 2
    Are you using the `using` statement aggressively to dispose objects? – Brad M May 24 '13 at 21:55
  • 1
    I recommend using a memory profiler - it should help illustrate the problem. – Reed Copsey May 24 '13 at 21:59
  • 1
    `I know that I have a memory leak`. How do you have reached this conclusion? – Steve May 24 '13 at 21:59
  • It is hard to tell without looking at code, but yes, suscribing to an event may prevent objects from being garbage collected Please look at John Skeet's answer [here](http://stackoverflow.com/questions/4526829/why-and-how-to-avoid-event-handler-memory-leaks) for more details of the potential problem. –  May 24 '13 at 22:03
  • Edited. The only events I am subscribing to are the buttons themselves. – user1947546 May 24 '13 at 22:44
  • Task Manager is not a memory profiler, and it can show memory being used consistently increasing when there is no leak. – Dour High Arch May 25 '13 at 00:40

4 Answers4

3

You're probably going to have to use a memory profiler to help you find the memory leak, if there really is one. One such tool that I have found useful in the past is the free CLR Profiler. You can also download its documentation. It will give you a picture of where memory is allocated, and how much of each type is allocated. It's pretty easy to use.

Another one I've seen elsewhere is http://memprofiler.com/, but that one isn't free (beyond the trial).

redtuna
  • 4,586
  • 21
  • 35
  • 1
    Another excellent choice is [ANTS Memory profiler](http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/). It's not free, but it has a trial period. – Thomas Levesque May 24 '13 at 22:49
2

There is nothing in your post that really screams "memory leak". Memory usage for a process can trickle upwards for other reasons, such as having objects waiting for garbage collection.

It's unclear from your question how much database access is going on, and what exactly "database access" means in this case. You may need to make sure your database updates are happening in a separate thread from the one handling your UI stuff. While it's possible to do database calls from your UI thread, it's not usually a good idea. That could be what's causing your slow-down.

You should also make sure your GPS access is also in a separate thread from your UI code. Creating 100 new objects a second isn't likely to cause memory problems but keep in mind that any calls which access hardware of some kind may take longer to process than you expect.

Last thing... why are you reading (or trying to) a GPS device 100 times per second? I can't think of any scenario where that makes any sense. From what I've heard, a typical GPS device updates only about 1-5 times per second. Reading it more often isn't going to provide any better results.

Mike Fulton
  • 920
  • 11
  • 22
  • I am profiling a road. Realistically, 10-20Hz is fine. I am unfamiliar with threads...the problem only happens after 10+ minutes. – user1947546 May 25 '13 at 19:16
0

Do you ever clear the text in the text box or do you just keep appending to it? Every time you add a new entry of length M to the next box and it already has a string of length N, it will create a new string of N + M length when performing the concatenation. If this updates very frequently and you never clear the text box, this will continuously accumulate allocated memory (much of it will be eligible for collection, but you will still see memory usage grow.)

To test this, try replacing the text rather than appending to it and see if that changes your observed results.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
  • This GREATLY reduces the memory usage growth. Its still growing, but at a much slower rate...thanks for the idea. – user1947546 May 25 '13 at 20:37
0

My answer to a popular question gives you various profiling options.

If you make use of those tools you should be able to find where your suspected memory leak occurs and make code changes to resolve them.

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102