[英]Reading large file - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. in C#
[英]Exception of type 'System.OutOfMemoryException' was thrown in C#
HtmlHelper.GetTagsAndValues(htmlContent);
我得到這個錯誤:
at System.String.Split(String[] separator, Int32 count, StringSplitOptions options)
at System.String.Split(String[] separator, StringSplitOptions options)
at WebCrawler.Logic.CrawlerManager.UseRulesOnHtmlPage(Agencies agency, String pageUrl, List`1 listTagValuePair, RulesGroups ruleGroup) in D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\CrawlerManager.cs:line 263
at WebCrawler.Logic.CrawlerManager.GetAdvertismentFromHtmlContent(List`1 listTagValuePair, Agencies agency, String pageUrl) in D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\CrawlerManager.cs:line 191
at WebCrawler.Logic.CrawlerManager.ImportAdvertisment2Database.Work(Crawler crawler, PropertyBag propertyBag) in D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\CrawlerManager.cs:line 668
at WebCrawler.Logic.CrawlerManager.ImportAdvertisment2Database.Process(Crawler crawler, PropertyBag propertyBag) in D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\CrawlerManager.cs:line 584
我讀了這篇文章:
我怎樣才能防止這個錯誤?
整個方法:
public static List<TagValuePair> GetTagsAndValues(string htmlContent)
{
List<TagValuePair> tagsValues = new List<TagValuePair>();
Dictionary<string, int> tagAppearance = new Dictionary<string, int>();
HtmlDocument doc = new HtmlDocument();
if (htmlContent != null)
{
doc.LoadHtml(htmlContent);
if (doc.DocumentNode.SelectNodes("//*") == null)
{
List<TagValuePair> tempList = new List<TagValuePair>();
tempList.Add(new TagValuePair("Error!", htmlContent, -1));
return tempList;
}
foreach (HtmlNode tag in doc.DocumentNode.SelectNodes("//*"))
{
try
{
if (!string.IsNullOrEmpty(tag.InnerHtml.Trim()))
{
if (!tagAppearance.Keys.Contains(tag.Name))
{
tagAppearance.Add(tag.Name, 1);
}
else
tagAppearance[tag.Name] = tagAppearance[tag.Name] + 1;
tagsValues.Add(new TagValuePair(tag.Name, tag.InnerHtml.Trim(), tagAppearance[tag.Name]));
}
else
{
// Help link: http://refactoringaspnet.blogspot.com/2010/04/using-htmlagilitypack-to-get-and-post_19.html
if (!string.IsNullOrEmpty(tag.GetAttributeValue("value", "").Trim()))
{
if (!tagAppearance.Keys.Contains("option value"))
{
tagAppearance.Add("option value", 1);
}
else
tagAppearance["option value"] = tagAppearance["option value"] + 1;
tagsValues.Add(new TagValuePair("option value", tag.GetAttributeValue("value", "").Trim(), tagAppearance["option value"]));
}
if (tag.NextSibling != null && !string.IsNullOrEmpty(tag.NextSibling.InnerHtml.Trim()))
{
if (!tagAppearance.Keys.Contains(tag.Name))
{
tagAppearance.Add(tag.Name, 1);
}
else
tagAppearance[tag.Name] = tagAppearance[tag.Name] + 1;
tagsValues.Add(new TagValuePair(tag.Name, tag.NextSibling.InnerHtml.Trim(), tagAppearance[tag.Name]));
}
}
}
catch (Exception)
{
return null;
}
}
}
編輯:
確切的錯誤在這里:
doc.LoadHtml(htmlContent);
我建議查看 memory 分析器,以確保您的應用程序中沒有任何泄漏。 鑒於您說它發生在應用程序工作 12 小時后,這似乎表明它可能是一個緩慢的泄漏,最終導致 OutOfMemory 異常。
您可以通過多種方式無意中保留會導致緩慢泄漏的引用。 運行分析器將幫助您識別這些問題。 它可能不是導致問題的一行代碼。 可能只是一行代碼經常向您展示壓倒駱駝的稻草。
我以前使用 過 Redgates Ants Profiler (它提供 14 天免費試用),它幫助我大量降低 memory 的使用率並提高性能。 我最近似乎經常使用它,但這純粹是因為我發現它是一個非常有價值的工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.