0

MSDN Web サイトから取得した以下のコードをご覧ください。

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 の新しいインスタンスがあります。これについて 2 つの質問があります。

  1. これらのインスタンスはどのように破壊されますか? すなわちc = Nothing。確かにメモリリークがありますか?
  2. TableRowTable クラスは、 andのインスタンスがどこにあるかをどのように知るの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)
    
4

1 に答える 1

0

.NET has a Garbage Collector that releases memory for you.

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

If you need to release memory immediately (such as with database connections), you can Dispose of them, as long as they implement IDisposable.

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.

于 2012-06-13T13:19:57.130 に答える