請看看下面的代碼,這是我從MSDN website採取:TableCell - 內存泄漏?
Sub Page_Load(sender As Object, e As EventArgs)
' Generate rows and cells.
Dim numrows As Integer = 3
Dim numcells As Integer = 2
Dim j As Integer
For j = 0 To numrows - 1
Dim r As New TableRow()
Dim i As Integer
For i = 0 To numcells - 1
Dim c As New TableCell()
c.Controls.Add(New LiteralControl("row " & j.ToString() & ", cell " & i.ToString()))
r.Cells.Add(c)
Next i
Table1.Rows.Add(r)
Next j
End Sub 'Page_Load
還有的TableRow的新實例爲每一行和的TableCell的新實例爲每個表單元格中創建。我對此有兩個問題:
- 這些實例如何被破壞?即
c = Nothing
。肯定有內存泄漏? Table類如何知道
TableRow
和TableCell
的實例在哪裏?我想問的原因是因爲它看來,你不能重用的TableCell
多個表格單元格和TableRow
多個錶行的情況下,即你不能做這樣的事情:Dim objTable As New Table Dim objTableRow As New TableRow Dim objTableCell As New TableCell objTableCell.Text = "Test Row 1 Cell 1" objTableRow.Cells.Add(objTableCell) objTableCell.Text = "Test Row 1 Cell 2" objTableRow.Cells.Add(objTableCell) objTable.Rows.Add(objTableRow) objTableCell.Text = "Test Row 2 Cell 1" objTableRow.Cells.Add(objTableCell) objTableCell.Text = "Test Row 2 Cell 2" objTableRow.Cells.Add(objTableCell) objTable.Rows.Add(objTableRow) objTableCell.Text = "Test Row 3 Cell 1" objTableRow.Cells.Add(objTableCell) objTableCell.Text = "Test Row 3 Cell 2" objTableRow.Cells.Add(objTableCell) objTable.Rows.Add(objTableRow)
我意識到,但根據我的經驗,總是值得儘可能地管理內存。我在.NET應用程序中遇到內存泄漏問題。 – w0051977
.NET不允許你像在C++/COM中一樣輕鬆地管理內存。根據我的經驗,.NET內存泄漏與不丟棄一次性對象直接相關。 – jrummell
你是說這樣做毫無意義? tc = Nothing – w0051977