开发者

possible memory leak?

i'm profiling the below code inside a singltone and found that a lot of Rate objects are kept in memory altough i clear them.

protected void FetchingRates()
{
  int count = 0;

  while (true)
  {
    try
    {
      if (m_RatesQueue.Count > 0)
      {
        List<RateLog> temp = null;

        lock (m_RatesQueue)
        {
          temp = new List<RateLog>();
          temp.AddRange(m_RatesQueue);
          m_RatesQueue.Clear();
        }

        foreach (RateLog item in temp)
        {
          m_ConnectionDataAccess.InsertRateLog(item);
        }

        temp.Clear();
        temp = null;
      }
      count++;
      Thread.Sleep(int.Parse(ConfigurationManager.AppSettings["RatesIntreval"].ToString()));
    }
    catch (Exception ex)
    {                   
    }
  }
} 

the insertion to the queue is made by:

public void InsertLogRecord(RateLog msg)
{
  try
  {
    if (m_RatesQueue != null)
    {
      //lock (((ICollection)m_queue).SyncRoot)
      lock (m_RatesQueue)
      {
        //insert new job to the line and release the thread to continue working.
        m_RatesQueue.Add(msg);
      }
    }
  }
  catch (Exception ex)
  {
  }
}

the worker inserts rate log into DB as follows:

 internal int InsertRateLog(RateLog item)
    {
        try
        {
            SqlCommand dbc = GetStoredProcCommand("InsertRateMonitoring");
            if (dbc == null)
                return 0;
            dbc.Parameters.Add(new SqlParameter("@HostName", item.HostName));
            dbc.Parameters.Add(new SqlParameter("@RateType", item.RateType));
            dbc.Parameters.Add(new SqlParameter("@LastUpdated", item.LastUpdated)开发者_如何转开发);
            return ExecuteNonQuery(dbc);
        }
        catch (Exception ex)
        {
            return 0;
        }
    }

any one sees a possible memory leak?


Stopping swallowing all exceptions would be the first place to start I would suggest.


You are certainly clearing the queue and the temporary list temp (which is unnecessary since it is eligible for collection even before you assign null to the reference). At this point I think your problem is more likely related to the following line.

m_ConnectionDataAccess.InsertRateLog(item);

You are passing a reference to a RateLog to another method. You have not provided any details on this method so I cannot eliminate the possibility that it is storing its own copy of the reference in a separate data structure.


I have experienced the same issue. There is probably a real explanation for this, but I couldn't find it.

I assumed that because I was in a while(true) loop the GC won't run. I don't know if this is an artefact of MS's implementation of the .NET framework (.NET 3.5), but it is what I experienced.

The way I mitigated the memory pile up was by putting GC.Collect(); at the bottom of the loop.

I have a feeling it was something to do with undisposed SqlConnection objects.


There is no need in clearing and nulling of List<RateLog> temp. It will be collected by GC anyway because leaving from function scope with lack of references, i.e. there is no more references to this variable on function end so it will be collected.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜