[concurrency-interest] DirectByteBuffers and reachabilityFence

Andrew Haley aph at redhat.com
Tue Dec 8 14:03:42 EST 2015


On 12/08/2015 06:28 PM, Vitaly Davidovich wrote:
>>
>> This warning about object lifetimes and finalizers is part of the
>> now-twenty-year-old Java Language Specification.  One would hope that
>> people have had time to get used to it and its implications.
> 
> We're going in circles now.  Let me just ask you this -- do you
> think Oracle would ship a JDK version where code, following the
> model of DBB, now breaks?

Absolutely, yes.  We already know that very similar code breaks.

> The problem is that there was no way to achieve the desired lifetime
> guarantees with today's and all prior JDK versions. 

Sure there is: write a field (a counter, say) in your methods and read
it in the finalizer.  Make sure that you do something with the field
in the finalizer to ensure it's not eliminated: a volatile write will
do.  This is fully JLS-compliant; reachabilityFence() is just an
optimization.

> Why was DBB released then if it's full of subtle gotchas? What are
> you going to say to users whose code will break, and it will break
> subtly under particular optimization/code shape scenarios? That they
> should've followed the 20+ yr old JLS spec, and now that
> reachabilityFence is out, they should sweep all their code and their
> 3rd party deps to ensure these subtle lifetimes issues are fixed?

Yes, more or less.  It's happened in the past, and it'll continue
to happen.  To begin with people didn't much have to worry about
subtleties to do with finalization and lifetimes, and were bitten
by it, and fixed their code.

Andrew.


More information about the Concurrency-interest mailing list