Dispose variables to free memory

Discussion in 'ASP .Net' started by Big George, Sep 6, 2010.

  1. Big George

    Big George Guest

    Hello,
    I don't get it how to use Dispose in ASP.NET 1.1.

    On MyClass, I put my own Dispose procedure. I instance MyClass as
    oMyClass, I use it and after I finish, I call oMyClass.Dispose,
    looking for releasing memory.

    Repeater1 stores and shows properties from oMyClass. But after doing
    oMyClass.Dispose, Repeater1 shows oMyClass.properties with no value

    - Am I doing wrong the dispose function?
    - If leave Dispose method on MyClass and don't call it, will .NET
    still dispose all variables or not?


    'On my aspx code behind I put:

    '''' Code behind
    myArray = New ArrayList

    Do While dataRead.Read
    oMyClass = New MyClass()
    myArray.Add(oMyClass)
    oMyClass.Dispose()
    Loop
    dataRead.Dispose

    Repeater1.DataSource = myArray
    Repeater1.DataBind()
    'Repetear1 shows properties from MyClass
    '''' Code behind


    '''' MyClass code:
    Public Class MyClass
    Implements IDisposable
    Private mproperty1 as string
    Private mproperty2 as string

    Private disposedValue As Boolean = False

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
    If disposing Then
    mproperty1=""
    mproperty2=""
    End If

    End If
    Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose

    Dispose(True)
    GC.SuppressFinalize(Me)
    End Sub

    Public Sub ProcedureToFillMyProperties
    ....
    End Sub

    Public ReadOnly Property Property1() As String
    Get
    Return mproperty1
    End Get
    End Property

    Public ReadOnly Property Property2() As String
    Get
    Return mproperty2
    End Get
    End Property

    End Class
     
    Big George, Sep 6, 2010
    #1
    1. Advertising

  2. Big George

    Cubaman Guest

    On Sep 6, 9:14 pm, Big George <> wrote:
    > Hello,
    > I don't get it how to use Dispose in ASP.NET 1.1.
    >
    > On MyClass, I put my own Dispose procedure. I instance MyClass as
    > oMyClass, I use it and after I finish, I call oMyClass.Dispose,
    > looking for releasing memory.
    >
    > Repeater1 stores and shows properties from oMyClass. But after doing
    > oMyClass.Dispose, Repeater1 shows oMyClass.properties with no value
    >
    > - Am I doing wrong the dispose function?
    > - If leave Dispose method on MyClass and don't call it, will .NET
    > still dispose all variables or not?
    >
    > 'On my aspx code behind I put:
    >
    > '''' Code behind
    > myArray = New ArrayList
    >
    > Do While dataRead.Read
    >    oMyClass = New MyClass()
    >    myArray.Add(oMyClass)
    >    oMyClass.Dispose()
    > Loop
    > dataRead.Dispose
    >
    > Repeater1.DataSource = myArray
    > Repeater1.DataBind()
    > 'Repetear1 shows properties from MyClass
    > '''' Code behind
    >
    > '''' MyClass code:
    > Public Class MyClass
    >    Implements IDisposable
    >    Private mproperty1 as string
    >    Private mproperty2 as string
    >
    >    Private disposedValue As Boolean = False
    >
    >    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    >             If Not Me.disposedValue Then
    >                 If disposing Then
    >                     mproperty1=""
    >                     mproperty2=""
    >                 End If
    >
    >             End If
    >             Me.disposedValue = True
    >    End Sub
    >
    >    Public Sub Dispose() Implements IDisposable.Dispose
    >
    >          Dispose(True)
    >          GC.SuppressFinalize(Me)
    >    End Sub
    >
    >    Public Sub ProcedureToFillMyProperties
    >       ....
    >    End Sub
    >
    >    Public ReadOnly Property Property1() As String
    >             Get
    >                 Return mproperty1
    >             End Get
    >    End Property
    >
    >    Public ReadOnly Property Property2() As String
    >             Get
    >                 Return mproperty2
    >             End Get
    >    End Property
    >
    > End Class


    Hello:
    Dispose is used in classes that holds unmanaged or high resource
    consuming members. Like DDBB connections, graphics objects, etc. If
    your class only holds a couple of strings, it's unnecessary to make it
    IDisposable. If you want, use .Net Reflector and take a look at how
    it's implemented in SqlConnection class, for example.
    http://www.red-gate.com/products/reflector/
    Best regards.
     
    Cubaman, Sep 7, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio Concepcion
    Replies:
    3
    Views:
    2,770
    Antonio Concepcion
    Feb 17, 2005
  2. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    428
    Keith Thompson
    Dec 27, 2007
  3. Andy Fish
    Replies:
    3
    Views:
    407
    Anthony Jones
    Aug 27, 2008
  4. george
    Replies:
    0
    Views:
    1,164
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    302
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page