G
Guest
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?
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?