Basic Question

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

    Basic Question

    How do you force destruction of an object?

    Will a simple "someObj = null" suffice?

    -----
    MyType someObj = new MyType();
    ...
    ...


    someObj = null; // <= Will this call the destructor?

    -----

    --

    Happy Hacking,
    Gaurav

    --------------------------------


  • Jon Skeet

    #2
    Re: Basic Question

    Gaurav Vaish <me@privacy.net > wrote:[color=blue]
    > How do you force destruction of an object?[/color]

    You don't.
    [color=blue]
    > Will a simple "someObj = null" suffice?[/color]

    No:

    1) There may be other references to the object
    2) The object won't be garbage collected until the garbage collector
    next collects the generation the object lives in.
    [color=blue]
    > -----
    > MyType someObj = new MyType();
    > ...
    > ...
    >
    >
    > someObj = null; // <= Will this call the destructor?[/color]

    No.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Gaurav Vaish

      #3
      Re: Basic Question

      If there are no other instances of the object then this should suffice.. am
      I right?

      --

      Happy Hacking,
      Gaurav

      --------------------------------


      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19d62d e49853c0859896f c@news.microsof t.com...[color=blue]
      > Gaurav Vaish <me@privacy.net > wrote:[color=green]
      > > How do you force destruction of an object?[/color]
      >
      > You don't.
      >[color=green]
      > > Will a simple "someObj = null" suffice?[/color]
      >
      > No:
      >
      > 1) There may be other references to the object
      > 2) The object won't be garbage collected until the garbage collector
      > next collects the generation the object lives in.
      >[color=green]
      > > -----
      > > MyType someObj = new MyType();
      > > ...
      > > ...
      > >
      > >
      > > someObj = null; // <= Will this call the destructor?[/color]
      >
      > No.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]

      Comment

      • Jon Skeet

        #4
        Re: Basic Question

        Gaurav Vaish <me@privacy.net > wrote:[color=blue]
        > If there are no other instances of the object then this should suffice.. am
        > I right?[/color]

        No. "No other instances of the object" doesn't make sense - an object
        *is* an instance. Did you mean if there are no other references to the
        object? If so, that's enough to make it eligible for garbage collection
        - but it doesn't mean that it will instantly be garbage collected, or
        that the finalizer will immediately be called.

        Also note that if it's a local variable, or if it's an instance field
        of another object which is going to become eligible for garbage
        collection, you don't need to set it to null.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Rob Windsor

          #5
          Re: Basic Question

          Gaurav,

          No, "someObj = null" will not suffice. .NET uses reference tracking garbage
          collection which means that your object will be destroyed some time from the
          point the last reference to it is released and the application terminates.

          Here's a link to a great article that describes this process in detail.



          --
          Rob Windsor
          G6 Consulting
          Toronto, Canada



          "Gaurav Vaish" <me@privacy.net > wrote in message
          news:bkh41u$1c6 qo$1@ID-108900.news.uni-berlin.de...[color=blue]
          > How do you force destruction of an object?
          >
          > Will a simple "someObj = null" suffice?
          >
          > -----
          > MyType someObj = new MyType();
          > ...
          > ...
          >
          >
          > someObj = null; // <= Will this call the destructor?
          >
          > -----
          >
          > --
          >
          > Happy Hacking,
          > Gaurav
          > http://gvaish.virtualave.net
          > --------------------------------
          >
          >[/color]


          Comment

          Working...