RFR 9: 8138696 : java.lang.ref.Cleaner (original) (raw)

RFR 9: 8138696 : java.lang.ref.Cleaner - an easy to use alternative to finalization

Daniel Fuchs daniel.fuchs at oracle.com
Wed Oct 21 13:58:18 UTC 2015


Hi Roger, Peter,

On 21/10/15 15:38, Roger Riggs wrote:

Hi Peter,

I've always assumed that the arguments could not be gc'd at least until the method returns in part because the caller was holding references. But I suppose in a completely inlined case an optimizer might try to make more relaxed conclusions. Since the new object has not yet been published, the reachability of objects in a constructor must have some additional rules to keep objects alive until it is published.

I've recently been bitten by a test that did the rough equivalent of this:

Object o = new Object();
ReferenceQueue<Object> queue = new ReferenceQueue<>();
WeakReference<Object> ref = new WeakReference(o, queue);
o = null;
System.gc();
WeakReference<?> wr = queue.remove();

The issue was that the reference 'ref' was sometime eagerly garbage collected before 'o', and the call to queue.remove() sometimes never returned. I should have known (because it had happened to me before) but these kind of issues are quite easy to miss, and have a propensity to wait for some weeks after integration before making themselves known...

I do hope that method parameters are not eagerly gc'ed before the method returns - that would be yet another thing to easily miss :-(

best regards,

-- daniel



More information about the core-libs-dev mailing list