.net 关于StackOverflowException的快速(希望)解决方案
我正在写一个.NETWebSpider。虽然它在我的一个站点(大约20页)上运行得很好,但它在我管理的另一个站点(大约500页)上使用System.StackOverflowException进行轰炸 我是在一台win7 64位i3笔记本电脑上开发的,它有8g内存、128g hyperx ssd和无交换文件 我的问题是。。。。我是否因为没有交换文件而引发此异常 cpu使用率(vs2010调试过程)仅为34%左右,ram使用率仅为74-75m 如果是这样的话,我怎样才能确保它不会发生 这是没有递归的 代码:.net 关于StackOverflowException的快速(希望)解决方案,.net,vb.net,stack-overflow,.net,Vb.net,Stack Overflow,我正在写一个.NETWebSpider。虽然它在我的一个站点(大约20页)上运行得很好,但它在我管理的另一个站点(大约500页)上使用System.StackOverflowException进行轰炸 我是在一台win7 64位i3笔记本电脑上开发的,它有8g内存、128g hyperx ssd和无交换文件 我的问题是。。。。我是否因为没有交换文件而引发此异常 cpu使用率(vs2010调试过程)仅为34%左右,ram使用率仅为74-75m 如果是这样的话,我怎样才能确保它不会发生 这是没有递归
您可能知道,此错误很可能是由于代码中的错误导致递归算法中的无限循环。虽然您说您不使用递归,但可能是无意中发生了递归 找出原因的最简单方法是附加调试器,将Visual Studio配置为在出现异常时中断,并在应用程序中触发错误
当错误发生且调试器中断时,请查看调用堆栈-希望您能看到问题所在。我猜可能是软件问题。StackOverflowException通常发生在递归算法出现问题时(尽管您提到您没有使用递归算法)。另一个常见原因是属性实现或相等性比较中的错误。 例如:
public string Name
{
set
{
Name = value;
}
}
我们能看一些示例代码吗?你能发布一些蜘蛛算法的伪代码吗?可能是由于没有堆栈跟踪,方法链调用了spider?-1。这是分析[通用可能来自任何地方]StackOverflowException所需的绝对最小值。现在是早上6点。你能等一下代码吗。我从我的手机上发布了这篇文章,希望它是一个简单的答案(如被问到的)顺便说一句。。。我没有;t最初添加代码是因为我读到(实际上是在stackoverflow.com上读到),这个问题可能是由于堆栈不够大引起的,如果机器交换文件太小,就会发生这种情况。。。因为我的是不存在的…(我不喜欢这个特定的递归错误…,而且事实上VS和ReSharper都没有为此对我大喊大叫。)
Private Function PopulateSEOList(Optional ByVal _Worker As ComponentModel.BackgroundWorker = Nothing) As List(Of Typing.SEO)
Dim _L = LinkList, _Ct As Long = 0
Dim _NL As New List(Of Typing.SEO)
Dim _EL As Typing.SEO.Elements = Nothing
Dim _Doc As HDocument = Nothing, _Keywords As String = String.Empty, _Description As String = String.Empty, _Content As HElement = Nothing
For i As Long = 0 To _L.Count - 1
Try
_Ct += 1
Using _HDoc As New Downloader
With _HDoc
_Doc = .DownloadHDoc(_L(i).SiteUrl)
End With
End Using
Tasks.Parallel.Invoke(Sub()
'Keywords
For Each Item In _Doc.Descendants("meta")
If Item.Attribute("name") = "keywords" Then
_Keywords = Item.Attribute("content").Value
'Exit For
End If
Next
End Sub,
Sub()
'Description
For Each Item In _Doc.Descendants("meta")
If Item.Attribute("name") = "description" Then
_Description = Item.Attribute("content").Value
'Exit For
End If
Next
End Sub,
Sub()
If _Doc.Descendants("body") IsNot Nothing Then
_Content = _Doc.Descendants("body").FirstOrDefault
End If
End Sub,
Sub()
_EL = New Typing.SEO.Elements() With {
.H1 = If(_Doc.Descendants("h1") IsNot Nothing, (From n In _Doc.Descendants("h1").AsParallel()
Select n.Value).ToList(), Nothing),
.H2 = If(_Doc.Descendants("h2") IsNot Nothing, (From n In _Doc.Descendants("h2").AsParallel()
Select n.Value).ToList(), Nothing),
.H3 = If(_Doc.Descendants("h3") IsNot Nothing, (From n In _Doc.Descendants("h3").AsParallel()
Select n.Value).ToList(), Nothing),
.H4 = If(_Doc.Descendants("h4") IsNot Nothing, (From n In _Doc.Descendants("h4").AsParallel()
Select n.Value).ToList(), Nothing),
.H5 = If(_Doc.Descendants("h5") IsNot Nothing, (From n In _Doc.Descendants("h5").AsParallel()
Select n.Value).ToList(), Nothing),
.H6 = If(_Doc.Descendants("h6") IsNot Nothing, (From n In _Doc.Descendants("h6").AsParallel()
Select n.Value).ToList(), Nothing),
.UL = If(_Doc.Descendants("ul") IsNot Nothing, (From n In _Doc.Descendants("ul").AsParallel()
Select n.Value).ToList(), Nothing),
.OL = If(_Doc.Descendants("ol") IsNot Nothing, (From n In _Doc.Descendants("ol").AsParallel()
Select n.Value).ToList(), Nothing),
.STRONG = If(_Doc.Descendants("strong") IsNot Nothing OrElse _Doc.Descendants("b") IsNot Nothing,
(From n In _Doc.Descendants("strong").AsParallel()
Select n.Value).Union(From n In _Doc.Descendants("b").AsParallel()
Select n.Value).ToList(), Nothing),
.BLOCKQUOTE = If(_Doc.Descendants("blockquote") IsNot Nothing, (From n In _Doc.Descendants("blockquote").AsParallel()
Select n.Value).ToList(), Nothing),
.EM = If(_Doc.Descendants("em") IsNot Nothing OrElse _Doc.Descendants("i") IsNot Nothing,
(From n In _Doc.Descendants("em").AsParallel()
Select n.Value).Union(From n In _Doc.Descendants("i").AsParallel()
Select n.Value).ToList(), Nothing),
.A = If(_Doc.Descendants("a") IsNot Nothing, (From n In _Doc.Descendants("a").AsParallel()
Select New Typing.SEO.Elements.Links() With {
.Content = n.Value,
.Title = If(n.Attribute("title") IsNot Nothing,
n.Attribute("title").Value,
Nothing),
.Target = If(n.Attribute("target") IsNot Nothing,
n.Attribute("target").Value,
Nothing),
.Rel = If(n.Attribute("rel") IsNot Nothing,
n.Attribute("rel").Value,
Nothing),
.Href = If(n.Attribute("href") IsNot Nothing,
n.Attribute("href").Value,
Nothing)
}).ToList(), Nothing),
.IMG = If(_Doc.Descendants("img") IsNot Nothing,
(From n In _Doc.Descendants("img").AsParallel()
Select New Typing.SEO.Elements.Images() With {
.Alt = If(n.Attribute("alt") IsNot Nothing,
n.Attribute("alt").Value,
Nothing),
.Source = If(n.Attribute("src") IsNot Nothing,
n.Attribute("src").Value,
Nothing),
.Title = If(n.Attribute("title") IsNot Nothing,
n.Attribute("title").Value,
Nothing)
}).ToList(),
Nothing)
}
End Sub)
_NL.Add(New Typing.SEO() With {
.Link = _L(i).SiteUrl,
.Title = _Doc.Descendants("title").First().Value,
.Keywords = _Keywords,
.Description = _Description,
.Content = _Content,
.ContentElements = _EL
})
_L.RemoveAt(i)
_EL = Nothing : _Doc = Nothing
ReportProgress((_Ct / _L.Count) * 100, _Worker)
Catch ex As Exception
'Put logging in here
End Try
Next
Return _NL
End Function
public string Name
{
set
{
Name = value;
}
}