-3

When I read the source code of Java AQS, I see the method acquireQueued with annotation help gc sets p.next = null.

If it releases memory for p, then why does it need to set p.next to null?

enter image description here

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55

1 Answers1

0

It means it's helping the garbage collector by setting that variable to null, so that its previous value will become unreachable (assuming it's not reachable through some other variable) and thus collectible, without having to establish the unreachability of p.

  • The Java VM doesn't use references, so values don't become unreferenced in that sense. It's not completely clear to me from this fragment whether `p` (and thus `p.next`) would be unreachable (and thus reclaimable) immediately after `setHead(node)`; if so, then setting `p.next` to `null` may not be necessary. – Daniel Pryden Oct 08 '18 at 02:12
  • Paradoxically, in fact, since `p.next` is mutated *after* the call to `setHead(node)`, it may in fact pin the node pointed to by `p` and *prevent* it from becoming unreachable any earlier. – Daniel Pryden Oct 08 '18 at 02:18
  • In fact, the real explanation is to do with cross-generational links / "floating garbage"; see dup. – Stephen C Oct 08 '18 at 02:25
  • @DanielPryden thanks for your answer – zheng cheng Oct 08 '18 at 04:20