RE: Memory leak with Datagrid and array




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
.



Relevant Pages

  • Re: Garbage Collection
    ... If you can explicitly call Dispose, it is always best to do this as this ... provides timely release of resources the object may be holding onto. ... > Sub Internal1() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Lowdown on finalization
    ... > private sub MySub() ... except in this case the Dispose is redundant... ... >> clean up an object that has overridden Finalize. ... >> resources will usually implement both IDisposable AND override finalize. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Confused about GC & Disposing: How to get it right?
    ... > class that uses instances of objects that have the dispose method, ... Should I conclude that every class that owns resources that have ... > dispose method should also use the Dispose Design Pattern? ... > Protected Overloads Overridable Sub Dispose ...
    (microsoft.public.dotnet.framework)
  • Re: Finalize and database connection... contradiction in msdn or a
    ... > | The Garbage Collector might do its stuff a number of times before ... > | sometime after LongRunningProcedure returns and the End Sub is executed ... > of when to Call Dispose, actually its now a list of when its OK not to ... > | Private Sub AAA ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Please Help. Questions about SQL in web form
    ... > OUTER JOIN Person Client RIGHT OUTER JOIN Job RIGHT OUTER JOIN Activity ... > datagrid to the returned DS. ... > Sub Button1_Click(ByVal sender As System.Object, ... > able to edit the data that is in both datatables, and I think the way to ...
    (microsoft.public.dotnet.framework.adonet)