Understanding the Dispose method and datasets?

Discussion in 'ASP .Net' started by Leon, Nov 12, 2004.

  1. Leon

    Leon Guest

    Are dataset automatically stored in memory?

    Does the dispose() method automatically dispose of the dataset in the code
    below?

    Do I have to dispose a dataset from memory or does the dataset dispose
    itself when not in use?

    I know I can use the clear method, but that just clear the data within
    dataset not dispose of the dataset all together right?

    *****Code Ex.
    Public Overloads Function RetrievePersonalization(ByVal AccountID As
    Integer) As DataRow

    Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    SqlDbType.Int, 4)}
    parameters(0).Value = AccountID

    Dim Account As DataSet
    Try
    Account = RunProcedure( "GetStudentPersonalization", parameters, "Account")
    Return Account.Tables(0).Rows(0)
    Account.Dispose()

    Finally
    If Not Account Is Nothing Then
    Account.Dispose()
    End If
    End Try

    End Function
    Leon, Nov 12, 2004
    #1
    1. Advertising

  2. Leon

    Scott Allen Guest

    Hi Leon:

    As far as memory is concerned, you do not have to Dispose any object.
    The CLR manages memory and will periodically reclaim unused memory
    with a garbage collector. This includes the DataSet and the data in a
    DataSet. Calling Dispose does not hurry this process along.

    Dispose is for unmanaged resources, like file handles and database
    connections. The rule of thumb is to call Dispose on any and every
    object that implements IDisposable. Unfortunately, the DataSet
    inherits from MarshalByValueComponent which gives the DataSet a
    Dispose method. In pratice we rarely Dispose a DataSet because it
    offers little benefit, and the DataSet often ends up in remoting and
    caching scenarios where you don't know when all clients are actually
    done with the DataSet

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/


    On Fri, 12 Nov 2004 09:56:49 -0600, "Leon" <> wrote:

    >Are dataset automatically stored in memory?
    >
    >Does the dispose() method automatically dispose of the dataset in the code
    >below?
    >
    >Do I have to dispose a dataset from memory or does the dataset dispose
    >itself when not in use?
    >
    >I know I can use the clear method, but that just clear the data within
    >dataset not dispose of the dataset all together right?
    >
    >*****Code Ex.
    >Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >Integer) As DataRow
    >
    >Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >SqlDbType.Int, 4)}
    >parameters(0).Value = AccountID
    >
    >Dim Account As DataSet
    >Try
    >Account = RunProcedure( "GetStudentPersonalization", parameters, "Account")
    >Return Account.Tables(0).Rows(0)
    >Account.Dispose()
    >
    >Finally
    >If Not Account Is Nothing Then
    >Account.Dispose()
    >End If
    >End Try
    >
    >End Function
    >
    >
    >
    Scott Allen, Nov 12, 2004
    #2
    1. Advertising

  3. Leon

    Leon Guest

    you are right thanks, but..
    is an arraylist a managed or unmanaged resource?

    "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    news:...
    > Hi Leon:
    >
    > As far as memory is concerned, you do not have to Dispose any object.
    > The CLR manages memory and will periodically reclaim unused memory
    > with a garbage collector. This includes the DataSet and the data in a
    > DataSet. Calling Dispose does not hurry this process along.
    >
    > Dispose is for unmanaged resources, like file handles and database
    > connections. The rule of thumb is to call Dispose on any and every
    > object that implements IDisposable. Unfortunately, the DataSet
    > inherits from MarshalByValueComponent which gives the DataSet a
    > Dispose method. In pratice we rarely Dispose a DataSet because it
    > offers little benefit, and the DataSet often ends up in remoting and
    > caching scenarios where you don't know when all clients are actually
    > done with the DataSet
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    >
    > On Fri, 12 Nov 2004 09:56:49 -0600, "Leon" <> wrote:
    >
    >>Are dataset automatically stored in memory?
    >>
    >>Does the dispose() method automatically dispose of the dataset in the code
    >>below?
    >>
    >>Do I have to dispose a dataset from memory or does the dataset dispose
    >>itself when not in use?
    >>
    >>I know I can use the clear method, but that just clear the data within
    >>dataset not dispose of the dataset all together right?
    >>
    >>*****Code Ex.
    >>Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >>Integer) As DataRow
    >>
    >>Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >>SqlDbType.Int, 4)}
    >>parameters(0).Value = AccountID
    >>
    >>Dim Account As DataSet
    >>Try
    >>Account = RunProcedure( "GetStudentPersonalization", parameters,
    >>"Account")
    >>Return Account.Tables(0).Rows(0)
    >>Account.Dispose()
    >>
    >>Finally
    >>If Not Account Is Nothing Then
    >>Account.Dispose()
    >>End If
    >>End Try
    >>
    >>End Function
    >>
    >>
    >>

    >
    Leon, Nov 12, 2004
    #3
  4. Leon

    Scott Allen Guest

    Memory is a managed resource.
    The memory an ArrayList uses will be managed.

    If the ArrayList held on to any unmanged resources, it would implement
    IDisposable (which it doesn't).

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Fri, 12 Nov 2004 10:40:43 -0600, "Leon" <> wrote:

    >you are right thanks, but..
    >is an arraylist a managed or unmanaged resource?
    >
    >"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    >news:...
    >> Hi Leon:
    >>
    >> As far as memory is concerned, you do not have to Dispose any object.
    >> The CLR manages memory and will periodically reclaim unused memory
    >> with a garbage collector. This includes the DataSet and the data in a
    >> DataSet. Calling Dispose does not hurry this process along.
    >>
    >> Dispose is for unmanaged resources, like file handles and database
    >> connections. The rule of thumb is to call Dispose on any and every
    >> object that implements IDisposable. Unfortunately, the DataSet
    >> inherits from MarshalByValueComponent which gives the DataSet a
    >> Dispose method. In pratice we rarely Dispose a DataSet because it
    >> offers little benefit, and the DataSet often ends up in remoting and
    >> caching scenarios where you don't know when all clients are actually
    >> done with the DataSet
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com/blogs/scott/
    >>
    >>
    >> On Fri, 12 Nov 2004 09:56:49 -0600, "Leon" <> wrote:
    >>
    >>>Are dataset automatically stored in memory?
    >>>
    >>>Does the dispose() method automatically dispose of the dataset in the code
    >>>below?
    >>>
    >>>Do I have to dispose a dataset from memory or does the dataset dispose
    >>>itself when not in use?
    >>>
    >>>I know I can use the clear method, but that just clear the data within
    >>>dataset not dispose of the dataset all together right?
    >>>
    >>>*****Code Ex.
    >>>Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >>>Integer) As DataRow
    >>>
    >>>Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >>>SqlDbType.Int, 4)}
    >>>parameters(0).Value = AccountID
    >>>
    >>>Dim Account As DataSet
    >>>Try
    >>>Account = RunProcedure( "GetStudentPersonalization", parameters,
    >>>"Account")
    >>>Return Account.Tables(0).Rows(0)
    >>>Account.Dispose()
    >>>
    >>>Finally
    >>>If Not Account Is Nothing Then
    >>>Account.Dispose()
    >>>End If
    >>>End Try
    >>>
    >>>End Function
    >>>
    >>>
    >>>

    >>

    >
    Scott Allen, Nov 12, 2004
    #4
  5. Leon

    Leon Guest

    So how exactly does the CLR know when to periodically reclaim unused memory
    with a garbage collector? or how does it know when you are using some object
    anymore?

    "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    news:...
    > Memory is a managed resource.
    > The memory an ArrayList uses will be managed.
    >
    > If the ArrayList held on to any unmanged resources, it would implement
    > IDisposable (which it doesn't).
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Fri, 12 Nov 2004 10:40:43 -0600, "Leon" <> wrote:
    >
    >>you are right thanks, but..
    >>is an arraylist a managed or unmanaged resource?
    >>
    >>"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    >>news:...
    >>> Hi Leon:
    >>>
    >>> As far as memory is concerned, you do not have to Dispose any object.
    >>> The CLR manages memory and will periodically reclaim unused memory
    >>> with a garbage collector. This includes the DataSet and the data in a
    >>> DataSet. Calling Dispose does not hurry this process along.
    >>>
    >>> Dispose is for unmanaged resources, like file handles and database
    >>> connections. The rule of thumb is to call Dispose on any and every
    >>> object that implements IDisposable. Unfortunately, the DataSet
    >>> inherits from MarshalByValueComponent which gives the DataSet a
    >>> Dispose method. In pratice we rarely Dispose a DataSet because it
    >>> offers little benefit, and the DataSet often ends up in remoting and
    >>> caching scenarios where you don't know when all clients are actually
    >>> done with the DataSet
    >>>
    >>> --
    >>> Scott
    >>> http://www.OdeToCode.com/blogs/scott/
    >>>
    >>>
    >>> On Fri, 12 Nov 2004 09:56:49 -0600, "Leon" <> wrote:
    >>>
    >>>>Are dataset automatically stored in memory?
    >>>>
    >>>>Does the dispose() method automatically dispose of the dataset in the
    >>>>code
    >>>>below?
    >>>>
    >>>>Do I have to dispose a dataset from memory or does the dataset dispose
    >>>>itself when not in use?
    >>>>
    >>>>I know I can use the clear method, but that just clear the data within
    >>>>dataset not dispose of the dataset all together right?
    >>>>
    >>>>*****Code Ex.
    >>>>Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >>>>Integer) As DataRow
    >>>>
    >>>>Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >>>>SqlDbType.Int, 4)}
    >>>>parameters(0).Value = AccountID
    >>>>
    >>>>Dim Account As DataSet
    >>>>Try
    >>>>Account = RunProcedure( "GetStudentPersonalization", parameters,
    >>>>"Account")
    >>>>Return Account.Tables(0).Rows(0)
    >>>>Account.Dispose()
    >>>>
    >>>>Finally
    >>>>If Not Account Is Nothing Then
    >>>>Account.Dispose()
    >>>>End If
    >>>>End Try
    >>>>
    >>>>End Function
    >>>>
    >>>>
    >>>>
    >>>

    >>

    >
    Leon, Nov 12, 2004
    #5
  6. Leon

    bruce barker Guest

    the arraylist is all managed, but can contain an object that uses unmanqged
    resources. for example an arraylist of SqlConnections.

    -- bruce (sqlwork.com)

    "Leon" <> wrote in message
    news:et$...
    | you are right thanks, but..
    | is an arraylist a managed or unmanaged resource?
    |
    | "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    | news:...
    | > Hi Leon:
    | >
    | > As far as memory is concerned, you do not have to Dispose any object.
    | > The CLR manages memory and will periodically reclaim unused memory
    | > with a garbage collector. This includes the DataSet and the data in a
    | > DataSet. Calling Dispose does not hurry this process along.
    | >
    | > Dispose is for unmanaged resources, like file handles and database
    | > connections. The rule of thumb is to call Dispose on any and every
    | > object that implements IDisposable. Unfortunately, the DataSet
    | > inherits from MarshalByValueComponent which gives the DataSet a
    | > Dispose method. In pratice we rarely Dispose a DataSet because it
    | > offers little benefit, and the DataSet often ends up in remoting and
    | > caching scenarios where you don't know when all clients are actually
    | > done with the DataSet
    | >
    | > --
    | > Scott
    | > http://www.OdeToCode.com/blogs/scott/
    | >
    | >
    | > On Fri, 12 Nov 2004 09:56:49 -0600, "Leon" <> wrote:
    | >
    | >>Are dataset automatically stored in memory?
    | >>
    | >>Does the dispose() method automatically dispose of the dataset in the
    code
    | >>below?
    | >>
    | >>Do I have to dispose a dataset from memory or does the dataset dispose
    | >>itself when not in use?
    | >>
    | >>I know I can use the clear method, but that just clear the data within
    | >>dataset not dispose of the dataset all together right?
    | >>
    | >>*****Code Ex.
    | >>Public Overloads Function RetrievePersonalization(ByVal AccountID As
    | >>Integer) As DataRow
    | >>
    | >>Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    | >>SqlDbType.Int, 4)}
    | >>parameters(0).Value = AccountID
    | >>
    | >>Dim Account As DataSet
    | >>Try
    | >>Account = RunProcedure( "GetStudentPersonalization", parameters,
    | >>"Account")
    | >>Return Account.Tables(0).Rows(0)
    | >>Account.Dispose()
    | >>
    | >>Finally
    | >>If Not Account Is Nothing Then
    | >>Account.Dispose()
    | >>End If
    | >>End Try
    | >>
    | >>End Function
    | >>
    | >>
    | >>
    | >
    |
    |
    bruce barker, Nov 12, 2004
    #6
  7. Leon

    Scott Allen Guest

    The CLR has plenty of rich metadata to draw from. It knows what
    variables are referencing objects in memory, and what types those
    objects are.

    Every application then has a set of 'roots', which are local variables
    or static variables that are referencing objects in memory. The JIT
    compiler and the CLR work together to understand when the roots are
    active, and when they go out of scope. From these roots the CLR can
    know what objects in memory are still "in play" and what objects can
    no longer trace back a chain of ownership to a root.

    It's hard to answer questions like "when" will the CLR collect,
    because it's really a low level detail. My understanding right now is
    that the collector waits to run until a specific area of the heap (gen
    0) is full. This is a hot area for optimizations and perf
    improvements, so expect some change some in nearly every release.

    Right now there are two different implementations of the garbage
    collector, one is optimized for multi-proc machines (packaged in
    MsCorSvr.dll) and one for uni-proc machines (packaged in
    MsCorWks.dll)- these are often referred to as the server and
    workstation garbage collectors. My understanding is this changes for
    2005, in that there is a single dll, but still multi-proc vs.
    single-proc implementations.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Fri, 12 Nov 2004 11:03:21 -0600, "Leon" <> wrote:

    >So how exactly does the CLR know when to periodically reclaim unused memory
    >with a garbage collector? or how does it know when you are using some object
    >anymore?
    >
    Scott Allen, Nov 12, 2004
    #7
  8. Leon

    Leon Guest

    Thank again Scott, you have been a tremendous help!
    I will return the favor by sharing the knowledge.

    "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    news:...
    > The CLR has plenty of rich metadata to draw from. It knows what
    > variables are referencing objects in memory, and what types those
    > objects are.
    >
    > Every application then has a set of 'roots', which are local variables
    > or static variables that are referencing objects in memory. The JIT
    > compiler and the CLR work together to understand when the roots are
    > active, and when they go out of scope. From these roots the CLR can
    > know what objects in memory are still "in play" and what objects can
    > no longer trace back a chain of ownership to a root.
    >
    > It's hard to answer questions like "when" will the CLR collect,
    > because it's really a low level detail. My understanding right now is
    > that the collector waits to run until a specific area of the heap (gen
    > 0) is full. This is a hot area for optimizations and perf
    > improvements, so expect some change some in nearly every release.
    >
    > Right now there are two different implementations of the garbage
    > collector, one is optimized for multi-proc machines (packaged in
    > MsCorSvr.dll) and one for uni-proc machines (packaged in
    > MsCorWks.dll)- these are often referred to as the server and
    > workstation garbage collectors. My understanding is this changes for
    > 2005, in that there is a single dll, but still multi-proc vs.
    > single-proc implementations.
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Fri, 12 Nov 2004 11:03:21 -0600, "Leon" <> wrote:
    >
    >>So how exactly does the CLR know when to periodically reclaim unused
    >>memory
    >>with a garbage collector? or how does it know when you are using some
    >>object
    >>anymore?
    >>

    >
    Leon, Nov 12, 2004
    #8
  9. Dispose is used to free unmanaged resources. I try always to call Dispose,
    then set the dataset to null such as Account = null. Otherwise, the dataset
    object still exist after the dispose and can still be called. Setting it to
    null let's the framework know that it can free up the object whenever it
    gets around to garbage collection. As to when the object is cleared out of
    memory is anyone's guess as it's totally up to the garbage collection and
    memory management of the framework itself.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "Leon" <> wrote in message
    news:...
    > Are dataset automatically stored in memory?
    >
    > Does the dispose() method automatically dispose of the dataset in the code
    > below?
    >
    > Do I have to dispose a dataset from memory or does the dataset dispose
    > itself when not in use?
    >
    > I know I can use the clear method, but that just clear the data within
    > dataset not dispose of the dataset all together right?
    >
    > *****Code Ex.
    > Public Overloads Function RetrievePersonalization(ByVal AccountID As
    > Integer) As DataRow
    >
    > Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    > SqlDbType.Int, 4)}
    > parameters(0).Value = AccountID
    >
    > Dim Account As DataSet
    > Try
    > Account = RunProcedure( "GetStudentPersonalization", parameters,
    > "Account")
    > Return Account.Tables(0).Rows(0)
    > Account.Dispose()
    >
    > Finally
    > If Not Account Is Nothing Then
    > Account.Dispose()
    > End If
    > End Try
    >
    > End Function
    >
    >
    >
    >
    Mark Fitzpatrick, Nov 12, 2004
    #9
  10. Leon

    Leon Guest

    Thanks Mark!
    "Mark Fitzpatrick" <> wrote in message
    news:...
    > Dispose is used to free unmanaged resources. I try always to call Dispose,
    > then set the dataset to null such as Account = null. Otherwise, the
    > dataset object still exist after the dispose and can still be called.
    > Setting it to null let's the framework know that it can free up the object
    > whenever it gets around to garbage collection. As to when the object is
    > cleared out of memory is anyone's guess as it's totally up to the garbage
    > collection and memory management of the framework itself.
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    > "Leon" <> wrote in message
    > news:...
    >> Are dataset automatically stored in memory?
    >>
    >> Does the dispose() method automatically dispose of the dataset in the
    >> code below?
    >>
    >> Do I have to dispose a dataset from memory or does the dataset dispose
    >> itself when not in use?
    >>
    >> I know I can use the clear method, but that just clear the data within
    >> dataset not dispose of the dataset all together right?
    >>
    >> *****Code Ex.
    >> Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >> Integer) As DataRow
    >>
    >> Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >> SqlDbType.Int, 4)}
    >> parameters(0).Value = AccountID
    >>
    >> Dim Account As DataSet
    >> Try
    >> Account = RunProcedure( "GetStudentPersonalization", parameters,
    >> "Account")
    >> Return Account.Tables(0).Rows(0)
    >> Account.Dispose()
    >>
    >> Finally
    >> If Not Account Is Nothing Then
    >> Account.Dispose()
    >> End If
    >> End Try
    >>
    >> End Function
    >>
    >>
    >>
    >>

    >
    >
    Leon, Nov 12, 2004
    #10
  11. Leon

    Scott Allen Guest

    The runtime is pretty smart about knowing when locals are no longer
    being used. For instance, the following program uses the WeakReference
    class to hold a reference to a DataSet. A WeakReference holds a
    reference, but the garbage collector will still reclaim an object's
    memory if the only outstanding root references are weak.

    [STAThread]
    static void Main(string[] args)
    {
    DataSet ds = new DataSet();
    WeakReference wr = new WeakReference(ds);

    DumpReference(wr);

    GC.Collect();

    DumpReference(wr);
    }

    static void DumpReference(WeakReference wr)
    {
    object o = wr.Target;
    if(o == null)
    {
    Console.WriteLine("WeakReference target is null");
    }
    else
    {
    Console.WriteLine("WeakReference points to a " + o.GetType());
    }
    }

    You have to compile this in Release mode to get the full benefits of
    optimization. The output should be:

    WeakReference points to a System.Data.DataSet
    WeakReference target is null

    So without setting the local variable to null the garbage collector
    has stepped in and taken back memory from the dataset. If you add the
    line:

    GC.KeepAlive(ds);

    to the end of the Main method, then both calls to DumpReference will
    choose the non-null path.

    In any case, setting global variables / statics and even class members
    to null can still be useful at times, but with locals the JIT can
    figure out quite a bit.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/


    On Fri, 12 Nov 2004 17:52:07 -0600, "Leon" <> wrote:

    >Thanks Mark!
    >"Mark Fitzpatrick" <> wrote in message
    >news:...
    >> Dispose is used to free unmanaged resources. I try always to call Dispose,
    >> then set the dataset to null such as Account = null. Otherwise, the
    >> dataset object still exist after the dispose and can still be called.
    >> Setting it to null let's the framework know that it can free up the object
    >> whenever it gets around to garbage collection. As to when the object is
    >> cleared out of memory is anyone's guess as it's totally up to the garbage
    >> collection and memory management of the framework itself.
    >>
    >> Hope this helps,
    >> Mark Fitzpatrick
    >> Microsoft MVP - FrontPage
    >>
    >> "Leon" <> wrote in message
    >> news:...
    >>> Are dataset automatically stored in memory?
    >>>
    >>> Does the dispose() method automatically dispose of the dataset in the
    >>> code below?
    >>>
    >>> Do I have to dispose a dataset from memory or does the dataset dispose
    >>> itself when not in use?
    >>>
    >>> I know I can use the clear method, but that just clear the data within
    >>> dataset not dispose of the dataset all together right?
    >>>
    >>> *****Code Ex.
    >>> Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >>> Integer) As DataRow
    >>>
    >>> Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >>> SqlDbType.Int, 4)}
    >>> parameters(0).Value = AccountID
    >>>
    >>> Dim Account As DataSet
    >>> Try
    >>> Account = RunProcedure( "GetStudentPersonalization", parameters,
    >>> "Account")
    >>> Return Account.Tables(0).Rows(0)
    >>> Account.Dispose()
    >>>
    >>> Finally
    >>> If Not Account Is Nothing Then
    >>> Account.Dispose()
    >>> End If
    >>> End Try
    >>>
    >>> End Function
    >>>
    >>>
    >>>
    >>>

    >>
    >>

    >
    Scott Allen, Nov 13, 2004
    #11
  12. Leon

    Leon Guest

    Thanks again, I see exactly what you are saying " I have been doing a little
    reading in the msdn library".

    "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    news:...
    > The runtime is pretty smart about knowing when locals are no longer
    > being used. For instance, the following program uses the WeakReference
    > class to hold a reference to a DataSet. A WeakReference holds a
    > reference, but the garbage collector will still reclaim an object's
    > memory if the only outstanding root references are weak.
    >
    > [STAThread]
    > static void Main(string[] args)
    > {
    > DataSet ds = new DataSet();
    > WeakReference wr = new WeakReference(ds);
    >
    > DumpReference(wr);
    >
    > GC.Collect();
    >
    > DumpReference(wr);
    > }
    >
    > static void DumpReference(WeakReference wr)
    > {
    > object o = wr.Target;
    > if(o == null)
    > {
    > Console.WriteLine("WeakReference target is null");
    > }
    > else
    > {
    > Console.WriteLine("WeakReference points to a " + o.GetType());
    > }
    > }
    >
    > You have to compile this in Release mode to get the full benefits of
    > optimization. The output should be:
    >
    > WeakReference points to a System.Data.DataSet
    > WeakReference target is null
    >
    > So without setting the local variable to null the garbage collector
    > has stepped in and taken back memory from the dataset. If you add the
    > line:
    >
    > GC.KeepAlive(ds);
    >
    > to the end of the Main method, then both calls to DumpReference will
    > choose the non-null path.
    >
    > In any case, setting global variables / statics and even class members
    > to null can still be useful at times, but with locals the JIT can
    > figure out quite a bit.
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    >
    > On Fri, 12 Nov 2004 17:52:07 -0600, "Leon" <> wrote:
    >
    >>Thanks Mark!
    >>"Mark Fitzpatrick" <> wrote in message
    >>news:...
    >>> Dispose is used to free unmanaged resources. I try always to call
    >>> Dispose,
    >>> then set the dataset to null such as Account = null. Otherwise, the
    >>> dataset object still exist after the dispose and can still be called.
    >>> Setting it to null let's the framework know that it can free up the
    >>> object
    >>> whenever it gets around to garbage collection. As to when the object is
    >>> cleared out of memory is anyone's guess as it's totally up to the
    >>> garbage
    >>> collection and memory management of the framework itself.
    >>>
    >>> Hope this helps,
    >>> Mark Fitzpatrick
    >>> Microsoft MVP - FrontPage
    >>>
    >>> "Leon" <> wrote in message
    >>> news:...
    >>>> Are dataset automatically stored in memory?
    >>>>
    >>>> Does the dispose() method automatically dispose of the dataset in the
    >>>> code below?
    >>>>
    >>>> Do I have to dispose a dataset from memory or does the dataset dispose
    >>>> itself when not in use?
    >>>>
    >>>> I know I can use the clear method, but that just clear the data within
    >>>> dataset not dispose of the dataset all together right?
    >>>>
    >>>> *****Code Ex.
    >>>> Public Overloads Function RetrievePersonalization(ByVal AccountID As
    >>>> Integer) As DataRow
    >>>>
    >>>> Dim parameters As SqlParameter() = { New SqlParameter("@AccountID",
    >>>> SqlDbType.Int, 4)}
    >>>> parameters(0).Value = AccountID
    >>>>
    >>>> Dim Account As DataSet
    >>>> Try
    >>>> Account = RunProcedure( "GetStudentPersonalization", parameters,
    >>>> "Account")
    >>>> Return Account.Tables(0).Rows(0)
    >>>> Account.Dispose()
    >>>>
    >>>> Finally
    >>>> If Not Account Is Nothing Then
    >>>> Account.Dispose()
    >>>> End If
    >>>> End Try
    >>>>
    >>>> End Function
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>

    >
    Leon, Nov 13, 2004
    #12
  13. Leon

    luxmit85

    Joined:
    Feb 12, 2008
    Messages:
    1


    The CLR manages memory and will periodically reclaim unused memory
    with a garbage collector. This includes the DataSet and the data in a DataSet.

    if i put dataset in session and i have set session expiration time to 8 hr. My code is below

    Dataset ds=new Dataset();
    ds = objDrawUser.DrawingSrch(objDrawSrch);
    Session["srchResult"] = ds;

    In my case Is garbage collector dispose dataset or is it consider dataset in use ?
    luxmit85, Feb 12, 2008
    #13
    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,705
    Antonio Concepcion
    Feb 17, 2005
  2. news.microsoft.com
    Replies:
    0
    Views:
    422
    news.microsoft.com
    Apr 12, 2006
  3. Francisco Garcia
    Replies:
    2
    Views:
    458
    Fran Garcia
    Apr 13, 2006
  4. news.microsoft.com
    Replies:
    0
    Views:
    180
    news.microsoft.com
    Apr 12, 2006
  5. Francisco Garcia
    Replies:
    3
    Views:
    238
    vincent
    Apr 13, 2006
Loading...

Share This Page