[concurrency-interest] non-volatile writes and reachability

Gil Tene gil at azul.com
Wed Feb 13 14:57:20 EST 2019

It is certainly possible to build something that would keep an object indefinitely reachable even after all references to it (in the heap and in programmatically declared local variables) have been nulled. See below. But in most practical situations a subsequent ordering operation will take care of reachability, as most java GCs leverage precise information about the location of references (in the heap and on the stack) to only consider actually-reachable-from-roots things reachable.

Class b {
	int x;

class A {
	boolean done = false;
	B b;
	A(B b) { this.b = b; }

static final A a = new A(new B());

Thread 1:
done = true;
a.b = null;

Thread 2:
while (!a.done) {
	if (a.b != null)
		total += a.b.x;

Thread 2 can remain in an infinitive loop,. and the instance originally referred to by a.b can remain reachable forever, because it can be validly transformed to the equivalent of:

boolean localDone = a.done;
B localB = a.b;
if (localB != null) {
	while (!localDone) {
		total += localB.x;
} else {
	while (!localDone) {

> On Feb 13, 2019, at 10:56 AM, Mark Falco via Concurrency-interest <concurrency-interest at cs.oswego.edu> wrote:
> Hi all,
> I have a question on how the JMM rules interact with GC rules around reachability.  Imagine I have object A which has a non-volatile field which is the only reference into some heavy weight object B.  I safely publish A to other threads such that at least two threads reference the A. If subsequently one of the threads nulls out that non-volatile field referencing B, would B still be considered reachable, and thus not eligible for GC?  I imagine the answer is yes in theory, but either no or unlikely in practice?
> My apologies if this has been previously asked and answered, I couldn't find it in the archives.
> thanks,
> Mark
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20190213/b397ef6f/attachment.sig>

More information about the Concurrency-interest mailing list