DataRow garbage collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cbmeeks

    DataRow garbage collection

    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

  • Laurent Bugnion

    #2
    Re: DataRow garbage collection

    Hi,

    cbmeeks wrote:
    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

    Comment

    • ignacio machin

      #3
      RE: DataRow garbage collection

      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.



      "cbmeeks" wrote:
      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
      >
      >

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: DataRow garbage collection

        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" <galasoft-lb@bluewin.chsc hreef in bericht
        news:OXZxMMDBHH A.3836@TK2MSFTN GP02.phx.gbl...
        Hi,
        >
        cbmeeks wrote:
        >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

        Comment

        Working...