本文介绍了在GPS项目中努力解决内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经潜伏了很长时间了,这是我第一次学习C#,并且碰到了一些砖墙,并且内存泄漏,我不知道该如何解决.以及它通常是一个很大的内存猪.

我正在编写一个应用程序,该应用程序使用GPS NEMA数据中的速度数据在汽车PC上做各种事情.我使用以下教程来帮助我掌握这一点;

http://www.codeproject.com/KB/cs/GpsMapping.aspx [ ^ ]

到目前为止,到目前为止,我已经能够更改timer1_Tick方法中的代码以适合所需的数据,并且效果很好.

Hi,

I''ve been lurking for ages now, while I''ve been studying C# for the first time, and I''ve hit a bit of a brick wall with a memory leak that I can''t figure out how to solve as well as it being a generally large memory hog.

I''m writing an application that uses the speed data from GPS NEMA data to do various things on PC''s used in cars. I used the following tutorial to help me get to grips with this;

http://www.codeproject.com/KB/cs/GpsMapping.aspx[^]

So far so good, I was able to alter the code in the timer1_Tick method to suit the data that I wanted and it''s working really well.

private void timer1_Tick(object sender, EventArgs e)
{
    if (serialPort1.IsOpen)
    {
        string data = serialPort1.ReadExisting();
        string[] strArr = data.Split('$');
        for (int i = 0; i < strArr.Length; i++)
        {
            string strTemp = strArr[i];
            string[] lineArr = strTemp.Split(',');
            if (lineArr[0] == "GPRMC")
            {
                try
                {
                    //Speed
                    Double GroundSpeed = Convert.ToDouble(lineArr[7]);
                    //Convert from knots to MPH                    
                    GroundSpeed = GroundSpeed * 1.15;
                    //Convert to a String for the textbox
                    GroundspeedMPH = Convert.toString(GroundSpeed);
                    //Display
                    SpeedTextbox.Text = GroundspeedMPH;
                }
                catch
                {
                    //Cannot Read GPS values
                    SpeedTextbox.Text = ("");
                }
            }
        }
    }
    else
    {
        SpeedTextbox.Text = ("");
    }
}




问题是,每次计时器计时时,我都会发生大量内存泄漏.经过数小时的阅读,我认为我正在内存中为诸如字符串数组,双精度和字符串之类的变量创建空间,但是并没有在每次方法完成后都将其从内存中删除.然后,当方法再次启动并且内存使用量继续增长时,将重新创建它们.

遗憾的是,我的应用程序运行良好(尽管尚未完全完成),但无法像这样使用. :(我对我读过的有关处理和完成的内容有些困惑,即使解决问题的方法我也不太了解如何使用.:confused:

顺便说一句,我对此完全是个菜鸟,所以如果我走远并且谈论绝对垃圾,请不要解雇我.我很想学习. :)




The problem is that I get a large memory leak every time the timer ticks. After some hours of reading I''m thinking that I am creating spaces in memory for variables like string arrays, doubles and strings but not removing them from memory every time the method has finished. Then they are created all over again when the methods starts again and the memory usage continues to grow.

It''s such a shame as my app is working well (although not quite finished) but is unusable like this. :( I''m a bit confused by what I''ve read about dispose and finalize and I don''t really understand how to use them, if that is even the way to fix this. :confused:

BTW, I''m a total noob at this so please don''t flame me if I''m miles out and talking absolute rubbish. I''m very keen to learn. :)

推荐答案




问题是,每次计时器计时时,我都会发生大量内存泄漏.经过数小时的阅读,我认为我正在内存中为诸如字符串数组,双精度和字符串之类的变量创建空间,但是并没有在每次方法完成后都将其从内存中删除.然后,当方法再次启动并且内存使用量继续增长时,将重新创建它们.

遗憾的是,我的应用程序运行良好(尽管尚未完全完成),但无法像这样使用. :(我对我读过的有关处理和完成的内容有些困惑,即使解决问题的方法我也不太了解如何使用.:confused:

顺便说一句,我对此完全是个菜鸟,所以如果我走远并且谈论绝对垃圾,请不要解雇我.我很想学习. :)




The problem is that I get a large memory leak every time the timer ticks. After some hours of reading I''m thinking that I am creating spaces in memory for variables like string arrays, doubles and strings but not removing them from memory every time the method has finished. Then they are created all over again when the methods starts again and the memory usage continues to grow.

It''s such a shame as my app is working well (although not quite finished) but is unusable like this. :( I''m a bit confused by what I''ve read about dispose and finalize and I don''t really understand how to use them, if that is even the way to fix this. :confused:

BTW, I''m a total noob at this so please don''t flame me if I''m miles out and talking absolute rubbish. I''m very keen to learn. :)


private void timer1_Tick(object sender, EventArgs e)
{
  timer1_Tick.Enable = false;
  try
  {
   // main code here
  }
  finally
  {
    timer1_Tick.Enable = true;
  }
}



另外,您还可以通过在滴答声结束时自己调用collection来确保垃圾回收.



Also you could ensure garbage collection by calling collection yourself at the end of a tick

GC.Collect()



这篇关于在GPS项目中努力解决内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 06:08
查看更多