On Mon, 7 Dec 2020 22:14:28 GMT, David Holmes <dhol...@openjdk.org> wrote:

>> Ok, I understand now. `ReferenceType.instances()` only counts objects 
>> "reachable for the purposes of garbage collection". This change in behavior 
>> does concern me a little bit. I think the expectation is that the instances 
>> created by `ClassType.newInstance()` will not show up in this count unless 
>> `disableCollection()` is called, even when under a "suspend all". Clearly 
>> that's the expectation of this test, so the question is whether or not it is 
>> a reasonable expectation.
>> 
>> Note that `ClassType.newInstance()` says nothing about the state of the 
>> returned object w.r.t. GC. It makes no mention of the need to call 
>> `disableCollection()` before resuming the VM, so I guess this gives us some 
>> wiggle room. However, the argument against the object being strongly 
>> reachable is comes from user asking the question "who has the strong 
>> reference that makes it strongly reachable?". It's not obvious to the user 
>> why there is a strong reference, and why it seemingly goes a way once 
>> `VM.resumeAll()` is called.
>> 
>> I still think overall this is the right approach (baring a better approach 
>> being presented), but we may need to include some spec clarifications, and 
>> be prepared for some push back if this breaks anything.
>
> I don't follow your reasoning here Chris. All ObjectReferences can be GC'd at 
> any time unless GC has been disallowed. So a reference create via newInstance 
> is no different to any other reference. If it is currently reachable then 
> instances() should return it. Are you treating "reachable for the purposes of 
> garbage collection" as-if it said "strongly reachable"? It doesn't so I think 
> you are reading too much into this. I think there is a lot of flexibility in 
> this API in terms of what it may return regarding weak references.

I read "reachable for the purposes of garbage collection" as not including 
objects reachable only via weak reference. So if the only reference to an 
object is a weak reference, which is normally what you have after calling 
`ClassType.newInstance()`, then the object is not considered reachable. At the 
very least, his is how `ReferenceType.instances()` is implemented, and is based 
on JVMTI 
[FollowReferences](https://docs.oracle.com/en/java/javase/14/docs/specs/jvmti.html#FollowReferences)().

So given that, the expectation would be that an object returned 
`ClassType.newInstance()` would not be counted by `ReferenceType.instances()` 
unless something is done to add a strong reference to the object, such as 
calling `ObjectReference.disableCollection()`. Now with Per's changes a strong 
reference is also created with doing a VM.suspend(). The test doesn't expect 
this behavior, and it's understandable why.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1595

Reply via email to