可能存在内存泄漏?
我正在对单音中的以下代码进行分析,发现尽管我清除了许多 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)
{
}
}
}
向队列的插入是通过以下方式进行的:
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)
{
}
}
工作人员将速率日志插入数据库,如下所示:
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;
}
}
有人看到可能的内存泄漏吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议首先停止接受所有异常。
Stopping swallowing all exceptions would be the first place to start I would suggest.
您当然正在清除队列和临时列表
temp
(这是不必要的,因为它有资格收集甚至在将null
分配给引用之前)。在这一点上,我认为您的问题更可能与以下行有关。您将对
RateLog
的引用传递给另一个方法。您尚未提供有关此方法的任何详细信息,因此我无法消除它将自己的引用副本存储在单独的数据结构中的可能性。You are certainly clearing the queue and the temporary list
temp
(which is unnecessary since it is eligible for collection even before you assignnull
to the reference). At this point I think your problem is more likely related to the following line.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.我也经历过同样的问题。对此可能有一个真正的解释,但我找不到。
我假设因为我处于
while(true)
循环中,GC 将不会运行。我不知道这是否是 MS 的 .NET 框架(.NET 3.5)实现的产物,但这就是我所经历的。我减轻内存堆积的方法是将 GC.Collect(); 放在循环的底部。
我有一种感觉,这与未处理的
SqlConnection
对象有关。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.无需清除和清空
List;温度。无论如何,它都会被 GC 收集,因为离开函数作用域时缺少引用,即在函数结束时不再有对此变量的引用,因此它将被收集。
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.