Reputation: 109
Consider the following sample code:
Dim ta as new LogsTableAdapter
Dim lstLogs as new List(of LogsRow)
lstLogs.AddRange(ta.GetData.ToList)
ta.Dispose
ta = nothing
lstLogs.clear
lstLogs = nothing
GC.Collect
GC.WaitForFullGCComplete()
GC.Collect
Following the execution of this code, using windbg, I can see that all of the LogsRow objects as well as objects representing the fields of those objects (strings, ints decimals, etc) remain resident in memory. There also is a LogsDataTable resident in memory as well.
I know that ta.GetData() returns a datatable. But since I am casting that to a list that I am adding onto another list, it seems like perhaps my approach is leaving the datatable and all of its rows in memory.
Also, clearing the list, and nulling it, does not seem to be freeing up the resources the list and the objects in it occupy.
So what's actually going on here, and how do I free up those resources?
Upvotes: 0
Views: 257
Reputation: 109
Thanks to the comment from TyCobb, I moved my list initialization code to a different method, and restructured to use .Fill instead of .GetData() and after that method is called, I perform the garbage collection manually, and can see that the memory is now being correctly freed up.
Here is how I am filling the lists now, in a separate sub:
Using taFiles As New dsIBETTableAdapters.tblInfoDataTableAdapter With {.Connection = IbetConn}
Using dtFiles As New dsIBET.tblInfoDataDataTable
taFiles.Fill(dtFiles)
lstFiles.AddRange(dtFiles.ToList)
End Using
End Using
Upvotes: 1