Understanding the Dispose method and datasets?

L

Leon

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
 
S

Scott Allen

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
 
S

Scott Allen

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).
 
L

Leon

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?
 
B

bruce barker

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

-- bruce (sqlwork.com)

| you are right thanks, but..
| is an arraylist a managed or unmanaged resource?
|
| | > 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/
| >
| >
| >
| >>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
| >>
| >>
| >>
| >
|
|
 
S

Scott Allen

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.
 
L

Leon

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

Mark Fitzpatrick

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
 
L

Leon

Thanks Mark!
Mark Fitzpatrick said:
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
 
S

Scott Allen

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.
 
L

Leon

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

Scott Allen said:
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/


Thanks Mark!
 
Joined
Feb 12, 2008
Messages
1
Reaction score
0
Scott Allen said:
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" <[email protected]> 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
>
>
>



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 ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top