difference between destructor and garbage collector

后端 未结 4 557
执念已碎
执念已碎 2020-12-31 19:18

I want to know is there any difference between destructor and garbage collector, destructor is used to dispose of all unused objects at the end of the lifetime of the applic

4条回答
  •  被撕碎了的回忆
    2020-12-31 19:31

    The garbage collector primarily works by copying all the objects it can find to a new part of RAM, and then nuking the old area; it neither knows nor cares whether there were five or 500,000 objects left behind. Note that in addition to finding all objects referred to by live strong references, the garbage collector can find a few other objects as well, including objects which override Finalize, objects used as monitor locks, objects targeted by WeakReference objects, etc. Before nuking the old area from orbit, the garbage collector must deal with any of 'special' objects it knows about that might still be sitting there.

    Among other things, the garbage collector has a list of all objects that have registered a finalizer; it will checks all objects on that list to see if they've yet been copied to the new memory area. If any are found that haven't been, they'll be removed from the list of objects with a registered finalizer and added to a list of objects whose Finalize method should be run as soon as practical. Once this has been done for all objects with a registered finalizer, any objects on the list of objects needing immediate finalization, as well as any object to which those objects holds a reference, will be copied to the new area.

提交回复
热议问题