0

我以前问过这个问题,没有真正的答案。有人可以帮忙吗?我在单例中分析以下代码,发现List<Rate>虽然我清除了很多 Rate 对象 ( ) ,但它们仍保留在内存中。

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)
    {  
      Logger.Log(ex);                 
    }
  }
} 

通过以下方式插入队列:

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)
  {
    Logger.Log(ex);  
  }
}

工作人员将速率日志插入数据库,如下所示:

 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)
        {
            Logger.Log(ex);
            return 0;
        }
    }

有人看到可能的内存泄漏吗?

4

4 回答 4

2
  1. 我希望您正确地处理 ADO.NET 对象。(这只是一个很好的做法。)
  2. 任何杂散引用都会阻止您的RateLog对象被 GC 收集。

RateLog我建议您从创建对象的位置开始查看您的代码,并记下保存引用的所有位置。这里有一些要考虑的事情。

  1. 对象是否RateLog订阅了任何事件?
  2. 您是否将一组RateLog对象保存在静态类的某个位置?

您还应该考虑在课堂上封装所有线程安全样板。

public sealed class WorkQueue<T>
{
    private readonly System.Collections.Generic.Queue<T> _queue = new System.Collections.Generic.Queue<T>();
    private readonly object _lock = new object();

    public void Put(T item)
    {
        lock (_lock)
        {
            _queue.Enqueue(item);
        }
    }


    public bool TryGet(out T[] items)
    {
        if (_queue.Count > 0)
        {
            lock (_lock)
            {
                if (_queue.Count > 0)
                {
                    items = _queue.ToArray();
                    _queue.Clear();
                    return true;
                }
            }
        }

        items = null;
        return false;
    }
}

这将使您的代码更清晰:

protected void FetchingRates()
{
    int ratesInterval = int.Parse(ConfigurationManager.AppSettings["RatesIntreval"].ToString());
    int count = 0;
    var queue = new WorkQueue<RateLog>();

    while (true)
    {
        try
        {
            var items = default(RateLog[]);
            if (queue.TryGet(out items))
            {
                foreach (var item in items)
                {
                    m_ConnectionDataAccess.InsertRateLog(item);
                }
            }
        }
        catch (Exception ex)
        {  
            Logger.Log(ex);                 
        }

        Thread.Sleep(ratesInterval);
        count++;
    }
} 
于 2010-09-10T16:52:16.977 回答
2

看起来您并没有处理SqlCommand挂在 RateLog 上的内容。

于 2010-09-10T16:55:21.507 回答
0

如何将temp创作移出循环。您可能不允许 GC 进行清理。

protected void FetchingRates()
{
  int count = 0;
  List<RateLog> temp = new List<RateLog>();

  while (true)
  {
    try
    {
      if (m_RatesQueue.Count > 0)
      {    
        lock (m_RatesQueue)
        {
          temp.AddRange(m_RatesQueue);
          m_RatesQueue.Clear();
        }

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

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

之后temp.Clear()您可以尝试添加GC.Collect();. 这不应该是永久的解决方案,但可以用于您的分析以查看对象是否最终被清理。如果没有,那么可能仍然在某处附加了参考或事件。

于 2010-09-10T16:49:51.083 回答
0

Clear() 函数解构列表。但是 RateLog 实例呢?他们的解构器被调用了吗?锁呢,也许这可以防止 RateLog 被删除。

于 2010-09-10T16:50:04.473 回答