Garbage Collection and Finalizers: Finer Points

后端 未结 3 1921
小蘑菇
小蘑菇 2020-12-05 08:23

In answering another question* on SO, and the subsequent comment discussion, I ran into a wall on a point that I\'m not clear on.

Correct me on any point where I\'m

3条回答
  •  醉话见心
    2020-12-05 09:15

    It's simplest to think of the garbage collector as dividing objects into four groups:

    1. Those which aren't reachable by any rooted object;
    2. Those which are reachable from a list of live finalizable objects, but not from any other rooted object;
    3. Those which are on the list of live finalizable objects, but are also reachable through some rooted object other than that list.
    4. Those which are not on the list of live finalizable objects, but are reachable via some rooted object other than that list.

    When the garbage-collector runs, objects of type #1 disappear. Objects of #2 get added to a list of objects needing imminent finalization and removed from the "live finalizable objects" list (thus becoming objects of category #4). Note that the list of objects needing finalization is a normal rooted reference, so objects on this list cannot be collected while they are on it, but if no other rooted reference is created by the time the finalizer is complete the object will move to category #1.

提交回复
热议问题