在GPS项目中努力解决内存泄漏问题 [英] struggling with memory leak in GPS project

查看:71
本文介绍了在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. :)

推荐答案

'); for ( int i = 0 ; i < strArr.Length; i ++) { 字符串 strTemp = strArr [i]; 字符串 [] lineArr = strTemp.Split(' ,' ); 如果(lineArr [ 0 ] == " GPRMC") { 尝试 { // 速度 Double GroundSpeed = Convert.ToDouble(lineArr [ 7 ]); // 从节转换为MPH GroundSpeed = GroundSpeed * 1 . 15 ; // 转换为文本框的字符串 GroundspeedMPH = Convert.toString(GroundSpeed); // 展示 SpeedTextbox.Text = GroundspeedMPH; } 捕获 { // 无法读取GPS值 SpeedTextbox.Text =(" ); } } } } 其他 { SpeedTextbox.Text =(" ); } }
'); 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. :)


我建​​议您采取以下措施来防止计时器滴答声再次进入
I would recommend the following to protect against the timer tick being reentrant
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()


感谢答复和评论.我没想到会这么快. :)

我认为是造成内存泄漏的计时器的原因是应用程序还没有真正执行其他任何操作,内存使用率似乎每秒钟增加一次(计时器的间隔),并且如果我禁用了该功能计时器并运行应用程序,没有内存泄漏.我已经下了结论还是说得通?

我将尝试ARopo的建议并将结果发布.

谢谢
尼克
Thanks for the replies and comments. I wasn''t expecting any so soon. :)

The reason that I think it''s the timer that is causing the memory leak is that the application doesn''t really do anything else yet, the memory usage appears to increase every second (the interval of the timer) and if I disable the timer and run the app there is no memory leak. Have I jumped to a conclusion or does that make sense?

I will try the suggested from ARopo and post the results.

Thanks
Nick


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆