RE: Memory leak with Datagrid and array
- From: "mark" <mark@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 29 Dec 2005 10:43:03 -0800
So, I close, dispose and set the datagrid=nothing. I also hear that I should
set Matrix_A = Nothing . Seems like quite a bit to flush out all the
resources tied-up in this form.
The .net help says "When a form is closed, all resources created within the
object are closed and the form is disposed. A recursive analysis of this
definition indicates that the Datagrid is closed ergo it is disposed and all
of the resources used by it are closed."
Are datagrids handled differently than typical resources here?
At this point I have the following sequence of statements
Matrix_A.Close()
Matrix_A.DataGrid1.DataSource = Nothing
Matrix_A.Dispose()
Matrix_A = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Is the above necessary and sufficient for annihilation of the form2
instance Matrix_A and for freeing all of the resources used by it?
Also, This instance of form2 is a child of form1 which the user could close
by clicking the close control in the top right corner. I'll need to override
form2.closing and include the extra clean-up code?
--
mark b
"Ken Tucker [MVP]" wrote:
> Hi,
>
> After you close matrix_a you should displose of it.
> Matrix_A.Dispose() The data is still in the datagrid so do not forget to set
> the datagrid datasource = nothing.
>
> Ken
> ---------------------
>
> "mark" wrote:
>
> > I have a simple windows application that has a function to read a csv file
> > and enter the values into an array A as double(,). Also, an instance of form
> > 2 (which has a DataGrid) is created and the values of A are used to populate
> > the grid via a DataView. The instance of form2 is called Matrix_A . The basic
> > layout is as follows:
> >
> > #Region "Globals
> > Dim A As Double(,)
> > Dim Matrix_A As form2
> > #End Region
> >
> > Private Sub OpenA()
> > OpenCsv() 'simple read-in procedure
> > populateA()
> > End Sub
> >
> > Private Sub populateA()
> > If Not Matrix_A Is Nothing Then
> > Matrix_A.Close()
> > End If
> > Matrix_A = New form2
> > array_to_grid(A, Matrix_A.DataGrid1)
> > Matrix_A.MdiParent = Me
> > Matrix_A.Show()
> > End Sub
> >
> > Sub array_to_grid(ByVal array(,) As Double, ByRef dg As DataGrid)
> > Try
> > Dim Rows As Integer = array.GetLength(0) - 1
> > Dim Cols As Integer = array.GetLength(1) - 1
> > Dim dta As New DataTable("matrix")
> > Dim dca(Cols) As DataColumn
> > Dim dra(Rows) As DataRow
> > Dim dv As New DataView(dta)
> > dv.AllowNew = False
> > 'loop in columns
> > Dim i As Integer = 0
> > Dim j As Integer = 0
> > For j = 0 To Cols - 1
> > dca(j) = New DataColumn("")
> > dca(j).DataType = System.Type.GetType("System.Single")
> > dta.Columns.Add(dca(j))
> > Next
> > 'create rows
> > For i = 0 To Rows - 1
> > dra(i) = dta.NewRow
> > For j = 0 To Cols - 1
> > dra(i)(j) = array(i + 1, j + 1)
> > Next j
> > Next i
> > 'add rows to table
> > For i = 0 To Rows - 1
> > dta.Rows.Add(dra(i))
> > Next
> > 'bind the dataview to the grid
> > dg.DataSource = Nothing
> > dg.DataSource = dv
> > Catch ex As Exception
> > MsgBox(ex.Message)
> > End Try
> > End Sub
> >
> > When I first start my program, I use about 19M memory (task mngr). When I
> > load a 2500X2500 matrix I use about 42M.
> >
> > I then execute the sub:
> >
> > Private Sub clear_Matrices()
> > Matrix_A.Close()
> > Erase A
> > GC.Collect() 'added later in attempt to solve problem
> > GC.WaitForPendingFinalizers() 'added later
> > End Sub
> >
> > I would expect the memory use to revert to 19M but it does not. It stays up
> > around 40M. I am not enough of an expert to properly interpret results from
> > CLRProfiler but it appears that a considerable amount of memory is still tied
> > up in dataview, datatable, and datacolumn stuff even after the instance of
> > form2 containing the datagrid has been closed.
> >
> > How can I make my application not leak memory?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > --
> > mark b
.
- Follow-Ups:
- References:
- Memory leak with Datagrid and array
- From: mark
- RE: Memory leak with Datagrid and array
- From: Ken Tucker [MVP]
- Memory leak with Datagrid and array
- Prev by Date: Re: accessing document object model (DOM) using vb and asp.net
- Next by Date: Re: accessing document object model (DOM) using vb and asp.net
- Previous by thread: Re: Memory leak with Datagrid and array
- Next by thread: Re: Memory leak with Datagrid and array
- Index(es):
Relevant Pages
|