抛出 System.OutOfMemoryException
我在代码中遇到了 system.outofmemory 异常:
While r1.Read()
menu1id = r1("id")
db.AddInParameter(command2, "@menu1id", DbType.Int32, menu1id)
r2 = db.ExecuteReader(command2)
command2.Parameters.Clear()
menu1heading = r1("Heading")
If r1("url") IsNot Nothing And r1("moduleid") = 0 Then
url = r1("url").ToString
If InStr(url, "file:///") > 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=")
builder.Append(r1("url"))
builder.Append(" target=")
builder.Append(r1("urltarget"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r1("url"))
builder.Append(" target=")
builder.Append(r1("urltarget"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
End If
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r1("id"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
End If
While r2.Read
menu2id = r2("id")
db.AddInParameter(command3, "@menu2id", DbType.Int32, menu2id)
r3 = db.ExecuteReader(command3)
command3.Parameters.Clear()
menu2heading = r2("Heading")
If r2("url") IsNot Nothing And r2("moduleid") = 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r2("url"))
builder.Append(" target=")
builder.Append(r2("urltarget"))
builder.Append(menu2heading)
builder.Append("</a>")
level2.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r2("id"))
builder.Append(">")
builder.Append(menu2heading)
builder.Append("</a>")
level2.Add(builder.ToString)
End If
While r3.Read
menu3heading = r3("Heading")
menu3id = r3("id")
If r3("url") IsNot Nothing And r3("moduleid") = 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r3("url"))
builder.Append(" target=")
builder.Append(r3("urltarget"))
builder.Append(">")
builder.Append(menu3heading)
builder.Append("</a>")
level3.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r3("id"))
builder.Append(">")
builder.Append(menu3heading)
builder.Append("</a>")
level3.Add(builder.ToString)
End If
End While
r3.Close()
End While
r2.Close()
End While
r1.Close()
End While
r0.Close()
请问您能告诉我如何诊断和修复此异常吗?多谢。
I am getting a system.outofmemory exception in my code:
While r1.Read()
menu1id = r1("id")
db.AddInParameter(command2, "@menu1id", DbType.Int32, menu1id)
r2 = db.ExecuteReader(command2)
command2.Parameters.Clear()
menu1heading = r1("Heading")
If r1("url") IsNot Nothing And r1("moduleid") = 0 Then
url = r1("url").ToString
If InStr(url, "file:///") > 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=")
builder.Append(r1("url"))
builder.Append(" target=")
builder.Append(r1("urltarget"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r1("url"))
builder.Append(" target=")
builder.Append(r1("urltarget"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
End If
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r1("id"))
builder.Append(">")
builder.Append(menu1heading)
builder.Append("</a>")
level1.Add(builder.ToString)
End If
While r2.Read
menu2id = r2("id")
db.AddInParameter(command3, "@menu2id", DbType.Int32, menu2id)
r3 = db.ExecuteReader(command3)
command3.Parameters.Clear()
menu2heading = r2("Heading")
If r2("url") IsNot Nothing And r2("moduleid") = 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r2("url"))
builder.Append(" target=")
builder.Append(r2("urltarget"))
builder.Append(menu2heading)
builder.Append("</a>")
level2.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r2("id"))
builder.Append(">")
builder.Append(menu2heading)
builder.Append("</a>")
level2.Add(builder.ToString)
End If
While r3.Read
menu3heading = r3("Heading")
menu3id = r3("id")
If r3("url") IsNot Nothing And r3("moduleid") = 0 Then
Dim builder As New StringBuilder
builder.Append("<a href=http://")
builder.Append(r3("url"))
builder.Append(" target=")
builder.Append(r3("urltarget"))
builder.Append(">")
builder.Append(menu3heading)
builder.Append("</a>")
level3.Add(builder.ToString)
Else
Dim builder As New StringBuilder
builder.Append("<a href=~/Default.aspx?id=")
builder.Append(r3("id"))
builder.Append(">")
builder.Append(menu3heading)
builder.Append("</a>")
level3.Add(builder.ToString)
End If
End While
r3.Close()
End While
r2.Close()
End While
r1.Close()
End While
r0.Close()
Please can you tell me how I go about diagnosing and fixing this exception? thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将逻辑更改为单个 sql 查询,一次性返回所有菜单项,然后迭代此数据集来构建菜单,效果会好得多。
您可以尝试
这会返回上述查询返回的所有数据,包括确定树结构的 GroupingID。您应该能够将结果加载到对象集合中,然后使用 LINQ 查询它们来构建菜单。
将数据复制到以下类中,然后对它们的列表使用 LINQ:
You would be a lot better off changing your logic to a single sql query that returns all of your menu items in one go and then iterating this dataset to build your menu.
You could just try
This returns all of the data that the above queries return, including the GroupingID which determines the tree structure. You should be able to load up the results into a collection of objects and then query them using LINQ to build your menu.
Copy your data into the following class annd then use LINQ on a list of them:
该方法使用大量字符串连接,每个字符串连接都会创建大量新的临时字符串对象,这些对象会耗尽堆空间。可以通过使用
StringBuilder
创建连接字符串。StringBuilder
在连接大量字符串时不仅可以提高内存效率,而且速度也会更快:注意:正如 David Neale 在评论中指出的,在这种情况下最好使用用于创建 HTML/XML 文档树的
TagBuilder
(或XmlWriter
)。The method uses a lot of string concatenations, each of them creating lots of new temporary string objects which use up your heap space. This can be avoided by using a
StringBuilder
to create the concatenated string. TheStringBuilder
works not only more memory-efficient when concatenating lots of strings, it will also be much faster:Note: As pointed out by David Neale in the comments, in this context it would be better to use a
TagBuilder
(or anXmlWriter
) to create the HTML/XML document tree.