By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Understanding the Dispose method and datasets?

P: n/a
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


Nov 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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" <vn*****@msn.com> 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


Nov 18 '05 #2

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

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:h1********************************@4ax.com...
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" <vn*****@msn.com> 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

Nov 18 '05 #3

P: n/a
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" <vn*****@msn.com> wrote:
you are right thanks, but..
is an arraylist a managed or unmanaged resource?

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:h1********************************@4ax.com.. .
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" <vn*****@msn.com> 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


Nov 18 '05 #4

P: n/a
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:5f********************************@4ax.com...
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" <vn*****@msn.com> wrote:
you are right thanks, but..
is an arraylist a managed or unmanaged resource?

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:h1********************************@4ax.com. ..
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" <vn*****@msn.com> 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

Nov 18 '05 #5

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

-- bruce (sqlwork.com)

"Leon" <vn*****@msn.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
| you are right thanks, but..
| is an arraylist a managed or unmanaged resource?
|
| "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
| news:h1********************************@4ax.com...
| > 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" <vn*****@msn.com> 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
| >>
| >>
| >>
| >
|
|
Nov 18 '05 #6

P: n/a
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" <vn*****@msn.com> 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?


Nov 18 '05 #7

P: n/a
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:so********************************@4ax.com...
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" <vn*****@msn.com> 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?

Nov 18 '05 #8

P: n/a
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" <vn*****@msn.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #9

P: n/a
Thanks Mark!
"Mark Fitzpatrick" <ma******@fitzme.com> wrote in message
news:eK*************@TK2MSFTNGP11.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #10

P: n/a
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" <vn*****@msn.com> wrote:
Thanks Mark!
"Mark Fitzpatrick" <ma******@fitzme.com> wrote in message
news:eK*************@TK2MSFTNGP11.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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



Nov 18 '05 #11

P: n/a
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:t3********************************@4ax.com...
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" <vn*****@msn.com> wrote:
Thanks Mark!
"Mark Fitzpatrick" <ma******@fitzme.com> wrote in message
news:eK*************@TK2MSFTNGP11.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.