我目前有一个类,我在其中存储一个静态集合,该集合在调用某些方法时获取添加和删除的对象。这是当前代码:
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),这是由于内存泄漏吗?这甚至是检查内存管理的充分方法吗?当我从集合中移除一个对象时,我需要处理它吗?