Does setting object reference to null help the GC in collection

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

    Does setting object reference to null help the GC in collection

    I am having this confusion going on in my head for sometime now. The
    question is whether setting the object reference to null (nothing)
    help the GC in collection in anyway?
    Can anyone shed some light on this? Is there any way to validate this
    proposition? Or is it just a myth...

    -Thanks,
    Shree
  • Peter Morris

    #2
    Re: Does setting object reference to null help the GC in collection

    I believe not, take this example

    object x = new Person();
    x.FirstName = "Peter";
    x.LastName = "Morris";

    //Position A

    ...lots of code here..

    //Position B
    x.FullName = x.FirstName + " " + x.LastName;

    //Position C

    ...lots of code here..


    The first thing to remember is that the GC can interrupt this method at any
    point. If it interrupts at position A it will not collect the value in "x"
    because the value is referenced later in the method. If it interupts at
    position C it will be able to collect the value in "x" because it is no
    longer used.

    In summary

    The object is only held onto in this example if the variable is referenced.
    The fact that the object is referenced by a variable is irrelevant if that
    variable is not itself referenced.



    Pete


    Comment

    • Brian Gideon

      #3
      Re: Does setting object reference to null help the GC in collection

      On Mar 26, 4:07 am, "Peter Morris"
      <peter[dot]morris(at)capab leobjects.comwr ote:
      I believe not, take this example
      >
      object x = new Person();
      x.FirstName = "Peter";
      x.LastName = "Morris";
      >
      //Position A
      >
      ..lots of code here..
      >
      //Position B
      x.FullName = x.FirstName + " " + x.LastName;
      >
      //Position C
      >
      ..lots of code here..
      >
      The first thing to remember is that the GC can interrupt this method at any
      point.  If it interrupts at position A it will not collect the value in "x"
      because the value is referenced later in the method.  If it interupts at
      position C it will be able to collect the value in "x" because it is no
      longer used.
      >
      In summary
      >
      The object is only held onto in this example if the variable is referenced..
      The fact that the object is referenced by a variable is irrelevant if that
      variable is not itself referenced.
      >
      Pete
      Strangley, it can be collected in position B halfway through the call
      to FullName as long as it's after any explicit or implicit use of the
      'this' pointer inside that property.

      Here is a link that explains why this might be important to you and
      gives a example of where you would use GC.KeepAlive.


      Comment

      Working...