1

我的代码中出现 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()

请您告诉我如何诊断和修复此异常?多谢。

4

2 回答 2

4

该方法使用了大量的字符串连接,它们中的每一个都创建了许多新的临时字符串对象,这些对象会占用你的堆空间。这可以通过使用 aStringBuilder创建连接字符串来避免。这些StringBuilder工作不仅在连接大量字符串时更节省内存,而且速度也会更快:

Dim builder As New StringBuilder()
builder.Append("<a href=")
builder.Append("r0("url"))
builder.Append("target=")
builder.Append(r0("urltarget"))
builder.Append(menu0heading)
builder.Append("</a>")

Dim str as String
str = builder.ToString()

注意:正如 David Neale 在评论中指出的那样,在这种情况下,最好使用 a TagBuilder(或 an XmlWriter)来创建 HTML/XML 文档树。

于 2010-06-22T09:00:08.080 回答
4

将逻辑更改为一次性返回所有菜单项的单个 sql 查询会更好,然后迭代此数据集以构建菜单。

你可以试试

SELECT id, DepartmentID, GroupingID, Heading, OrderID, 
Publish, moduleid, url, urltarget
FROM Grouping 
WHERE (DepartmentID = 0 AND Publish <> 0)
ORDER BY OrderID

这将返回上述查询返回的所有数据,包括确定树结构的 GroupingID。您应该能够将结果加载到对象集合中,然后使用 LINQ 查询它们以构建您的菜单。

将您的数据复制到以下类中,然后在它们的列表中使用 LINQ:

public class DataClass
{
public string Id { get; set; }
public string DepartmentID { get; set; }
public string GroupingID { get; set; }
public string Heading { get; set; }
public string OrderID { get; set; }
public string Publish { get; set; }
public string Moduleid { get; set; }
public string Url { get; set; }
public string Urltarget { get; set; }

public List<DataClass> Children { get; set; }

public DataClass(string id, string departmentID, string groupingID, string heading, string orderID, string publish, string moduleid, string url, string urltarget)
{
    Id = id;
    DepartmentID = departmentID; 
    GroupingID = groupingID;
    Heading = heading;
    OrderID = orderID; 
    Publish = publish;
    Moduleid = moduleid;
    Url = url;
    Urltarget = urltarget;
}
}
于 2010-06-22T09:21:05.163 回答