嗨,我有一个 Outlook com 插件,它正在为我做一些简单的搜索技巧。我正在将它放在一起,但我遇到了内存不足的问题。这个过程非常简单,基本上循环通过一个outlook文件夹检查每个mailItem是否匹配。给定循环每次我都希望垃圾收集器跟上时重新初始化变量,但是当我观察内存时,它会丢失约 10 米/秒,直到系统内存不足并且我得到未处理的异常。
这是代码的一部分
private void FindInFolder(Outlook.MAPIFolder FolderToSearch)
{
Outlook.MailItem mailItem;
Outlook.MAPIFolder ParentFolder;
int counter = 0;
StatusBar.Text = "Searching in Folder " + FolderToSearch.FolderPath + "/" + FolderToSearch.Name;
StatusBar.Update();
this.Update();
foreach (COMObject item in FolderToSearch.Items)
{
counter++;
if (counter % 100 == 0)
{
StatusBar.Text = FolderToSearch.FolderPath + "/" + FolderToSearch.Name + " item " + counter + " of " + FolderToSearch.Items.Count;
StatusBar.Update();
if (counter % 1000 == 0)
{
GC.Collect();
}
}
if (item is Outlook.MailItem)
{
mailItem = item as Outlook.MailItem;
if (IsMatch(mailItem))
{
if (mailItem.Parent is Outlook.MAPIFolder)
{
ParentFolder = mailItem.Parent as Outlook.MAPIFolder;
ResultGrd.Rows.Add(mailItem.EntryID, ParentFolder.FolderPath, mailItem.SenderName, mailItem.Subject, mailItem.SentOn);
}
}
}
mailItem = null;
}
}
哪个电话
private Boolean IsMatch(Outlook.MailItem inItem)
{
Boolean subBool = false;
Boolean NameBool = false;
try
{
if (null != inItem)
{
if (SubjectTxt.Text != "")
{
if (inItem.Subject.Contains(SubjectTxt.Text))
{
subBool = true;
}
}
else
{
subBool = true;
}
if (NameTxt.Text != "")
{
if (inItem.Sender != null)
{
if (inItem.Sender.Name.Contains(NameTxt.Text))
{
NameBool = true;
}
}
}
else
{
NameBool = true;
}
return subBool && NameBool;
}
}
catch (System.Runtime.InteropServices.COMException ce)
{
if (ce.ErrorCode == -2147467259)
{
//DO nothing just move to the next one
}
else
{
MessageBox.Show("Crash in IsMatch error code = " + ce.ErrorCode + " " + ce.InnerException);
}
}
return false;
}
请原谅底部的所有错误捕获部分和 GC.collect 它们是我尝试找出错误并释放内存的一些尝试。
另请注意 FindInFolder 由一个新线程调用,因此我可以在它继续搜索时与结果进行交互。
到目前为止我已经尝试过:
使变量局部于函数而不是类,因此可以由 G 检索,但是“item”中最常用的变量,因为它是 foreach 的一部分,因此必须以这种方式声明。
每 1000 个邮件项执行一次手动 GC,这根本没有区别。
出于某种原因,它需要大量内存,只是循环遍历这些项目,而 GC 永远不会释放它们。
另请注意,我使用的是 netoffice 而不是 VSTO 用于 Com 插件。