GC and Dispose method questions

From: Teresa (Teresa_at_discussions.microsoft.com)
Date: 01/12/05


Date: Wed, 12 Jan 2005 08:21:04 -0800

1) If I do want to keep an object alive throughout the live of an
application, how can I ensure that the GC doesn't clean it up?

2a) How do I determine if an object is a managed or an unmanged resource? I
understand the basic definition for managed resources are resources that the
CLR manage and unmanged resources are resources that the CLR doesn't manage,
however, I haven't been able to find a concrete answer as to what resources
are manage or not manage by the CLR.

2b) This is a following question to 2a. Are String and Int32 managed or
unmanaged resources (since it's not possible to do sTemp.dispose or
iTemp.dispose)? So does doing sTemp = Nothing or iTemp = Nothing release the
resource back to the memory?

3) I created serveral classes and I would like to be able to control when I
dispose it. I've found several articles regarding this topic and in each of
these discussion, the solution is exactly identical. Below is the sample from
the Microsoft MSDN website and is it true that all I need to do is customize
the code below in each of class? Is that the correct implementation?

Public Class Base
    Implements IDisposable
    ' Implement IDisposable.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            ' Free other state (managed objects).
        End If
        ' Free your own state (unmanaged objects).
        ' Set large fields to null.
    End Sub

    Protected Overrides Sub Finalize()
        ' Simply call Dispose(False).
        Dispose(False)
    End Sub
End Class



Relevant Pages

  • Re: Garbage Collection
    ... If you can explicitly call Dispose, it is always best to do this as this ... provides timely release of resources the object may be holding onto. ... > Sub Internal1() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: MissingManifest Error in VB
    ... > Hi Daniel, ... >> debugger for the real names of your resources ... >>> Dim rm As System.Resources.ResourceManager ... >>> Public Sub New ...
    (microsoft.public.dotnet.framework.compactframework)
  • RE: Memory leak with Datagrid and array
    ... So, I close, dispose and set the datagrid=nothing. ... The .net help says "When a form is closed, all resources created within the ... > Matrix_A.DisposeThe data is still in the datagrid so do not forget to set ... >> Private Sub OpenA() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: MissingManifest Error in VB
    ... The 2 real name of resources are: ... "Daniel Moth" wrote: ... >> Public Sub New ... >> Private Sub MenuItem2_Click(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: GC and Dispose method questions
    ... on a .NET proj, I really appreciate all your quick responses. ... >> understand the basic definition for managed resources are resources that ... >> CLR manage and unmanged resources are resources that the CLR doesn't ... > One effect is that unmanaged resources do not pressure the GC for collection ...
    (microsoft.public.dotnet.languages.vb)