我目前有一个类,我存储一个静态集合,在调用某些方法时添加和删除对象。这是当前的代码:
public class MatchMaker : Hub
{
private static HashSet<SoloUser> soloUsers = new HashSet<SoloUser>();
//Client Requests
public void findNewPartner(string Name, string Country)
{
SoloUser soloUser = soloUsers.Users.FirstOrDefault(s => (s.Name == Name) && (s.Country == Major));
if (soloUsers.Users.Count > 0){
Clients.partnerRequestResult(soloUsers.Users.FirstOrDefault());
soloUsers.Users.Remove(soloUser);
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
Debug.WriteLine("TotalMemoryUsed: " + totalBytesOfMemoryUsed);
}
else
{
soloUser = new SoloUser {
Name = Name,
Country = Country
};
soloUsers.Users.Add(soloUser);
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
Debug.WriteLine("TotalMemoryUsed: " + totalBytesOfMemoryUsed);
}
}
}
当我跑步时:
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
Debug.WriteLine("TotalMemoryUsed: " + totalBytesOfMemoryUsed);
从集合中添加或删除对象totalBytesOfMemoryUsed
的输出变得越来越大(每次2mb),无论我是否从集合中添加或删除对象,这是由于内存泄漏?这甚至是检查内存管理的充分方法吗?当我从集合中删除对象时,是否需要处置对象?
答案 0 :(得分:1)
此处可能没有足够的信息来评论内存泄漏,但在一般情况下 - 请相信Garbage Collector。
如果您仍怀疑内存泄漏,请使用内存分析器(例如ANTS或dotTrace)。
答案 1 :(得分:0)