Results 1 to 6 of 6

Thread: finalize and dispose?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    finalize and dispose?

    sorry to sound stupid here.
    I am wondering what the difference is and what each one does?

    Does finalize tell the GC to get the objects to perhaps be disposed of, to be ready in some que?

    does dispose get rid of the objects/release the used memory?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: finalize and dispose?

    Yup.

    Quote Originally Posted by [url=http://msdn2.microsoft.com/en-us/library/system.object.finalize.aspx]System.Object.Finalize()[/url]
    The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.
    Finalize is called automatically by the framework. If you need to dump an object you should call its Dispose() method.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: finalize and dispose?

    sweet, i was right :P

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: finalize and dispose?

    Also, setting all references to an object to null has no immediate effect, it simply means it will be picked up by the GC on its next run.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: finalize and dispose?

    Finalize, or a destructor in C#, is a fail-safe. It is invoked by the garbage collector to ensure that the Dispose method of an object is executed even if the code that created it did not do so explicitly. If you do not call an object's Dispose method explicitly then it will take multiple passes of the garbage collector to fully clean up that object.

    Once no references to an object exist, that object becomes eligible for garbage collection. When the garbage collector feels like it, it will run through the list of all eligible objects. If they have been disposed or have no Dispose method then their memory is reclaimed and they're done. If they have a Dispose method that has not been called then the garbage collector invokes the Finalize method or destructor to dispose the object. They will then have to wait until the next time the garbage collector runs to have their memory reclaimed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width