locked
Strongly Typed Dataset Memory Leak RRS feed

  • Question

  • I am working on a project, and have defined a strongly typed dataset.  Normally I create a single instance of a dataset and use through out an application.  In this case I have need to clone the dataset, and clear 2 of the table and then fill those 2 tables with fake data for creating a sample report. at the end of the retuine, I dispose of the dataset.  So I was think all was good.  Well after running the samples a few tiems i noticed that the memory usage was going up everytime I called this method.  I did some poking around with a profiler and found out that there were still references to the dataset from within the dataset.  So i started writing code(see below) to clean up the references.  I have been able to remove most but i'm still left with 3 that I can not seem to get rid of, the DataRelationCollection+DataSetRelationCollection, DataTableCollection, and then the calling  reference (in the demo case Form1).  Has anyone managed to clean up a strongly typed dataset completely?

    To recreate:
    Create a new form and drop 2 buttons on it.  Then creates a strongly typed dataset with a tableadapter called TestDS
    Add the clean up code to the TestDS.vb file.
    Add the Form1 to the Form1.vb file.


    CleanUp Code:
    Partial Class TestDS
    
        Public Shadows Sub Dispose()
            MyBase.Dispose()
    
            Dim y As Integer
            Dim x As Integer
    
            For y = Me.Tables.Count - 1 To 0 Step -1
                Me.Tables(y).PrimaryKey = Nothing
                For x = Me.Tables(y).Columns.Count - 1 To 0 Step -1
                    Me.Tables(y).Columns(x).Dispose()
                    Me.Tables(y).Columns.RemoveAt(x)
                Next
                Me.Tables.RemoveAt(y)
            Next
    
            For y = Me.Relations.Count - 1 To 0 Step -1
                Me.Relations.RemoveAt(y)
            Next
            MyBase.Tables.Clear()
    
            RemoveHandler MyBase.Tables.CollectionChanged, AddressOf Me.SchemaChanged
            RemoveHandler MyBase.Relations.CollectionChanged, AddressOf Me.SchemaChanged
    
        End Sub
    
    End Class
    

    Demo Code:
    Public Class Form1
    
        Private _DS As TestDS
      
        Private Sub CreateDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateDS.Click
            Me._DS = New TestDS
        End Sub
    
        Private Sub DisposeDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisposeDS.Click
            Me._DS.Dispose()
            Me._DS = Nothing
        End Sub
    
    End Class
    

    Adam - Please remember to mark the answers.
    Monday, June 1, 2009 12:10 PM

Answers

  • Well after some more research I found my problem, i was not disposing the tables, just removing them.  for anyone that runs into this in the future here is the code:

    Partial Class TestDS
    
        Public Shadows Sub Dispose()
    
            Dim y As Integer
            Dim x As Integer
    
            For y = Me.Tables.Count - 1 To 0 Step -1
                Me.Tables(y).PrimaryKey = Nothing
                For x = Me.Tables(y).Columns.Count - 1 To 0 Step -1
                    Me.Tables(y).Columns(x).Dispose()
                    Me.Tables(y).Columns.RemoveAt(x)
                Next
                Me.Tables(y).Dispose()
                Me.Tables.RemoveAt(y)
            Next
    
            For y = Me.Relations.Count - 1 To 0 Step -1
                Me.Relations.RemoveAt(y)
            Next
            MyBase.Tables.Clear()
    
            RemoveHandler MyBase.Tables.CollectionChanged, AddressOf Me.SchemaChanged
            RemoveHandler MyBase.Relations.CollectionChanged, AddressOf Me.SchemaChanged
    
            MyBase.Dispose()
    
        End Sub
    
    End Class

    I hope this saves someone alot of time.

     


    Adam - Please remember to mark the answers.
    Monday, June 1, 2009 12:29 PM