开发者

handling the exception as System.OutOfMemoryException without try-catch

i have developed the C# application cre开发者_开发知识库ating the number of threads, After some time elapsed it throws exception as System.OutOfMemoryException when creating a new Thread.any idea to handle this exception.without using try - catch. how to increased the space in memory or how to release the thread from memory.this thing handle very fast .

private void DataChangeHandler(object sender, DataChangeEventArgs e)
        {         
            try
            {         update3(e);              
            }
            catch { }
        }

 public void update3(object h)
         {
             try
             {                                  
                     if (h != null)
                     {
                         DataChangeEventArgs e = (DataChangeEventArgs)h;
    OdbcConnection con128 = new OdbcConnection(LocalConnection.GetLocalConnetionString());
                         int counter = 0;
                         OdbcCommand cmd;
                         string UpdateQuery = "";
                         string query1 = "";
                         while (counter < e.sts.Length)
                         {
                             object val = e.sts[counter].DataValue;
                             int hour = e.sts[counter].TimeStampNet.Hour;
                             int minute = e.sts[counter].TimeStampNet.Minute;
                             int second = e.sts[counter].TimeStampNet.Second;
                             int millisecond = e.sts[counter].TimeStampNet.Millisecond;
                             int year = e.sts[counter].TimeStampNet.Year;
                             int month = e.sts[counter].TimeStampNet.Month;
                             int day = e.sts[counter].TimeStampNet.Day;
       DateTime sdate = new DateTime(year, month, day, hour, minute, second, millisecond);
                             string date = sdate.ToString("dd-MM-yyyy HH:mm:ss.fff");
                             DateTime dt = DateTime.FromFileTime(e.sts[counter].TimeStamp);
                             query1 += "select '" + val + "' as DTvalue ,'" + date + "' as DTdatelogged1,'" + OpcGroup.QualityToString(e.sts[counter].Quality) + "' as DTquality ,'" + dt + "' as DTtimestamp ,'" + e.sts[counter].HandleClient + "' as DTparamID Union " + Environment.NewLine;
                             counter++;
                         }

                         query1 = query1.Remove(query1.LastIndexOf("Union"));
                         UpdateQuery = "Update parameter t Left join " + Environment.NewLine;
                         UpdateQuery += " ( " + query1 + " ) Temp on" + Environment.NewLine;
                         UpdateQuery += "t.itemID=Temp.DTparamID" + Environment.NewLine;
                         UpdateQuery += "set paramvalue=DTvalue,date_logged1=DTdatelogged1,Quality=DTquality,date_logged=DTtimestamp " + Environment.NewLine;
                         UpdateQuery += "where t.itemID=Temp.DTparamID ";
                         if (con128.State == ConnectionState.Closed)
                             con128.Open();

                         cmd = new OdbcCommand(UpdateQuery, con128);
                         cmd.ExecuteNonQuery();

                         if ((con128.State == ConnectionState.Connecting) || (con128.State == ConnectionState.Open))
                         {
                             con128.Close();
                         }

                     }            


             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);

             }
         }

the data change handler invoke at every seconds this done by OPC server like timer


Instead of creating a huge number of threads, you should keep control and tracks of what you create. Use a pool of threads for example, or even better the Task Parallel Library.

Second thought is that you should not try to recover from OOM exceptions, you should do everything upfront to avoid it in the first place.


I highly doubt anyone will be able to give you the 'right' answer to what amounts to a very generic question with no source code. The best I can do is give a few generic pointers:

  • Make sure you are disposing of as many out of scope objects as possible.
  • Use the Thread Pool object as it will only allocate as many threads as the system can handle.
  • Use a memory profiler to see what objects are taking up the most space.


Now that you have posted some code:

The code doesn't show anywhere the creation of the many threads you mention however there are a few things to note:

  1. Both OdbcCommand and OdbcConnection are IDisposable and should therefor be wrapped into using in order to make sure any (unamanged) resources are properly released.
  2. Consider using string.Format() instead of concatenation to avoid creating many temporary strings. I doubt it will really be a problem but it's a bit cleaner this way.

Other than that your code doesn't look like it contains any memory leaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜