Dispose variables to free memory

  • Thread starter Thread starter Big George
  • Start date Start date
B

Big George

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,
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.
 
Back
Top