i have researched this for the past day and am posting here in case someone can help
i have an application that has user controls that contain a DataGridView. Each DGV is bound to a binding source and the binding source is an ES collection that gets reloaded at each refresh. I noticed that each time i refresh the grid, the amount of memory in use goes up. I ran a memory profiler and i can see things like DataRows, DataGridTextBoxCell, etc counts continue to climb while the number of rows in the collection/bindingsource/DGV is fairly low. i have tried all different kinds of disposes, etc and i cannot get this memory to ever be released.
the only thing i have not tried is to destroy and recreate the grid each time
here is the pertinent code:
RefreshData gets called either when a timer runs down or a user clicks a button/
Code:
Private problemColl As BusinessObjects.CallRequestCollection
Private probBindSource As BindingSource
Private Sub RefreshData()
Dim procName As String = "RefreshData"
Dim currentRow As String = String.Empty
Dim problems As BusinessObjects.CallRequestQuery
Dim client As BusinessObjects.ClientQuery
Dim intNotes As BusinessObjects.InternalNotesQuery
Dim patResults As BusinessObjects.PatientresultsQuery
Dim singleOC As BusinessObjects.SingleocAcnQuery
Timer1.Stop()
Me.Cursor = Cursors.WaitCursor
Me.Focus()
uxFromDate.MaxDate = Today
uxThruDate.MaxDate = Today
currentAccession = String.Empty
Dim sortCol As New DataGridViewColumn
sortCol = probGrid.SortedColumn
Dim sortOrder As New System.Windows.Forms.SortOrder
If Not sortCol Is Nothing Then
sortOrder = probGrid.SortOrder
Else
sortCol = probGrid.Columns("ProblemStatus")
sortOrder = Windows.Forms.SortOrder.Ascending
End If
Try
DirectCast(Me.ParentForm, MainForm).DisplayStatus("Refreshing Problem Accessions... " & Now)
If Not IsNothing(problemColl) Then
If problemColl.Count > 0 Then
currentRow = probGrid.CurrentRow.Cells(Enums.ProbGrid.Accession).Value.ToString()
End If
problemColl.Dispose()
problemColl = Nothing
probGrid.DataSource =
probGrid.Rows.Clear()
probGrid.Refresh()
End If
problemColl = New BusinessObjects.CallRequestCollection
problems = New BusinessObjects.CallRequestQuery("p")
client = New BusinessObjects.ClientQuery("c")
intNotes = New BusinessObjects.InternalNotesQuery("n")
patResults = New BusinessObjects.PatientresultsQuery("r")
singleOC = New BusinessObjects.SingleocAcnQuery("s")
problems.Select(problems.CallRequestID, problems.RequestNumber, _
problems.ProblemCreated, problems.ProblemStatusID, problems.OrderSiteID, _
problems.Accession, client.Name, client.Officephone, problems.PatientName, _
problems.IsStatSlab, singleOC.Collectdate, patResults.Result, intNotes.Notes)
problems.InnerJoin(singleOC).On(problems.Accession = singleOC.Accession)
problems.LeftJoin(patResults).On(problems.Accession = patResults.Accession)
problems.InnerJoin(client).On(problems.OrderClientID = client.Clientid)
problems.LeftJoin(intNotes).On(problems.Accession = intNotes.Accession)
problems.Where(problems.HasProblem = 1)
If uxProblemStatusSpecific.SelectedIndex <> -1 Then
problems.Where(problems.ProblemStatusID = _
CType(uxProblemStatusSpecific.SelectedValue.ToString(), Integer))
Else
If uxProblemStatus.SelectedIndex <> -1 Then
problems.Where(problems.ProblemStatusID <= _
CType(uxProblemStatus.SelectedValue.ToString(), Integer))
End If
End If
problems.Where(singleOC.Ordercode = "PROB")
problems.Where(patResults.Ordercode = "PROB")
If uxProblemStatusSpecific.SelectedIndex > -1 Then
If uxProblemStatusSpecific.SelectedValue.ToString() = "3" Then
problems.Where(problems.ProblemCreated.Between(CType(uxFromDate.Value.Date & " 00:00:00.000", DateTime), _
CType(uxThruDate.Value.Date & " 23:59:59.997", DateTime)))
End If
End If
If uxProblemStatus.SelectedIndex > -1 Then
If uxProblemStatus.SelectedValue.ToString() = "3" Then
problems.Where(problems.ProblemCreated.Between(CType(uxFromDate.Value.Date & " 00:00:00.000", DateTime), _
CType(uxThruDate.Value.Date & " 23:59:59.997", DateTime)))
End If
End If
problems.OrderBy(problems.ProblemCreated.Ascending)
problemColl.Load(problems)
mformLoading = True
If probBindSource IsNot Nothing Then
probBindSource = Nothing
'probBindSource.Dispose()
GC.Collect()
GC.WaitForPendingFinalizers()
End If
probBindSource = New BindingSource
probBindSource.DataSource = problemColl
mformLoading = False
probGrid.DataSource = Nothing
Debug.WriteLine(GC.GetTotalMemory(False))
GC.Collect()
Debug.WriteLine(GC.GetTotalMemory(False))
probGrid.DataSource = probBindSource
probGrid.Refresh()
If sortOrder = Windows.Forms.SortOrder.Ascending Then
probGrid.Sort(sortCol, System.ComponentModel.ListSortDirection.Ascending)
Else
probGrid.Sort(sortCol, System.ComponentModel.ListSortDirection.Descending)
End If
If problemColl.Count > 0 Then
If currentRow.Length > 0 Then
Dim gridRowIndex As Integer = probBindSource.Find("Accession", currentRow)
If gridRowIndex <> -1 Then
probGrid.CurrentCell = probGrid.Rows(gridRowIndex).Cells(Enums.ProbGrid.Accession)
probGrid.Rows(gridRowIndex).Selected = True
currentAccession = CType(probGrid.Rows(gridRowIndex).Cells(Enums.ProbGrid.Accession).Value, String)
Else
probGrid.CurrentCell = probGrid.Rows(0).Cells(Enums.ProbGrid.Accession)
probGrid.Rows(0).Selected = True
currentAccession = CType(probGrid.Rows(0).Cells(Enums.ProbGrid.Accession).Value, String)
End If
Else
probGrid.CurrentCell = probGrid.Rows(0).Cells(Enums.ProbGrid.Accession)
probGrid.Rows(0).Selected = True
currentAccession = CType(probGrid.Rows(0).Cells(Enums.ProbGrid.Accession).Value, String)
End If
LoadTests()
LoadComments()
End If
GetLastUpdate(slabTime, misysTime, nowTime)
KryptonHeaderGroup1.ValuesSecondary.Heading = problemColl.Count & " Problems Displayed - Last SLAB Update: " & slabTime
KryptonHeaderGroup1.ValuesSecondary.Description = "Next Refresh: " & DateAdd(DateInterval.Second, timeLeft, Now)
Catch ex As Exception
AppHandler.AddLogEntry("E", MODNAME & "::" & procName, 0, ex.Message.ToString(), ex.Source.ToString())
AppHandler.MailError(MODNAME, procName, ex)
AppHandler.ShowError(errMsgUnhandled, "Unhandled Exception Detected")
Finally
Me.Cursor = Cursors.Default
DirectCast(Me.ParentForm, MainForm).DisplayStatus(String.Empty)
Timer1.Interval = 1000
timeLeft = GetTimeLeft(settingProbCheckMFDay, settingProbCheckMFNight, settingProbCheckSSDay, settingProbCheckSSNight)
Timer1.Start()
Me.ActiveControl = probGrid
For Each f As Form In imageForms
If f.WindowState <> FormWindowState.Minimized Then
f.Activate()
End If
Next
End Try
problems = Nothing
Debug.WriteLine(GC.GetTotalMemory(False))
GC.Collect()
Debug.WriteLine(GC.GetTotalMemory(False))
End Sub