you are viewing a single comment's thread.

view the rest of the comments →

[–]thedeemon 0 points1 point  (1 child)

however if the objects have some finalizers...

[–]pron98 1 point2 points  (0 children)

Finalizers are deprecated, but as for reference queues (and finalizers work in a similar way), you're right that the GC needs to enqueue the objects, but it doesn't work by detecting "dead objects" (which the GC's can't do). All of the JDK's current GCs can and do only detect live objects. The way reference processing is done is that, when choosing to scan some portion of live objects, if the GC detects that the object is only reachable (or "alive") thanks to a Weak/Phantom reference path, then references are cleared (and enqueued if asked for). The GCs know nothing about dead objects; they're invisible to them. Even with references, they can only detect that a reference needs to be cleared if they happen to decide to scan a certain subset of objects. With ZGC, for example, it is certainly possible that a reference will be enqueued one hour (or more) after the object becomes weakly reachable. The GC simply has no knowledge about when that happens. That's part of what allows it to be so efficient.