DataRow garbage collection

C

cbmeeks

I think I know the answer to this but I would like to confim.

I have an app where I've built a custom DataSet containing several
DataTables.

Within the DataTables, I sometimes check for existing rows. I do this
like this:

DataRow[] dr = dtTable.Select("Category='VHS'");

if(dr.Length > 0)
......

This works fine. However, I am worried about de-allocating the DataRow
(dr) object. There is no Dispose method so should I explicitly set dr
= null every time or just not worry about it? In the same fasion that
I don't worry about de-allocating strings, ints, etc.

Thanks
cb
 
L

Laurent Bugnion

Hi,
I think I know the answer to this but I would like to confim.

I have an app where I've built a custom DataSet containing several
DataTables.

Within the DataTables, I sometimes check for existing rows. I do this
like this:

DataRow[] dr = dtTable.Select("Category='VHS'");

if(dr.Length > 0)
.....

This works fine. However, I am worried about de-allocating the DataRow
(dr) object. There is no Dispose method so should I explicitly set dr
= null every time or just not worry about it? In the same fasion that
I don't worry about de-allocating strings, ints, etc.

Thanks
cb

The garbage collector will clear the resource only when no more
reference to the object can be found. That's true for any object, so
it's the same as for strings. ints are different, because they are value
types, not reference types.

However, you don't control the garbage collector. It's very difficult to
influence when the memory will be freed. Setting the reference to null
can help, depending how you use the variable. However, for local
variable, with a limited scope, it doesn't make a difference. When the
variable comes out of scope, the reference is lost, and unless another
object has another reference to the same instance, the instance will be
marked for garbage collection.

Calling Dispose on a IDisposable object doesn't set the reference to
null. The Dispose method is there to clean resources, for example
closing DB connections, closing files, etc... The Dispose method is very
important, because even if you set the reference to null, this doesn't
clean the resources. So it's really another issue here.

HTH,
Laurent
 
G

Guest

HI,

You do not need to worry about it, as soon as dr is out of scope it's
marked for collection. The DataRows will not be collected as they are still
being referenced by the DataTable.
 
C

Cor Ligthert [MVP]

Laurent,

Of course it makes differences, setting something to null takes time,
whatever that is.

By instance people who don't like unboxing because of the needed time should
definitely not do that in my idea. (I am not in that last category, but I am
seldom making programs by instance in the image area). However I set seldom
something to null, in my idea does a good made managed code program not need
that.

Just as addition for your for the rest in my idea excellent message.

Cor

Laurent Bugnion said:
Hi,
I think I know the answer to this but I would like to confim.

I have an app where I've built a custom DataSet containing several
DataTables.

Within the DataTables, I sometimes check for existing rows. I do this
like this:

DataRow[] dr = dtTable.Select("Category='VHS'");

if(dr.Length > 0)
.....

This works fine. However, I am worried about de-allocating the DataRow
(dr) object. There is no Dispose method so should I explicitly set dr
= null every time or just not worry about it? In the same fasion that
I don't worry about de-allocating strings, ints, etc.

Thanks
cb

The garbage collector will clear the resource only when no more reference
to the object can be found. That's true for any object, so it's the same
as for strings. ints are different, because they are value types, not
reference types.

However, you don't control the garbage collector. It's very difficult to
influence when the memory will be freed. Setting the reference to null can
help, depending how you use the variable. However, for local variable,
with a limited scope, it doesn't make a difference. When the variable
comes out of scope, the reference is lost, and unless another object has
another reference to the same instance, the instance will be marked for
garbage collection.

Calling Dispose on a IDisposable object doesn't set the reference to null.
The Dispose method is there to clean resources, for example closing DB
connections, closing files, etc... The Dispose method is very important,
because even if you set the reference to null, this doesn't clean the
resources. So it's really another issue here.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 

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

Top