<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">> The reason for keeping a reference to virtual threads by default is not finalization but observability. The cost of holding a strong reference is near zero because we can rely on the same strong references in constructs such as Executors and StructuredTaskScope, while adding WeakReferences would come at a cost.<br></div><div dir="ltr"><br></div><div>Got it.<br>Observability needs reference. As VT can be plenty, they are strongly referenced, to avoid the cost of weak references. </div><div>Comparing, VT created by Executors are observable because Executors are referenced weakly by jdk.internal.vm.ThreadContainers#CONTAINER_REGISTRY<br><br></div><div>I deduce that number of Executors is expected to be smaller than the number of VTs and the cost of weak reference is acceptable.</div><div><br></div><div>I can't help feeling that VT could be freely GC'able, but when it was realized that observability without strong reference is costly, narration was changed and GC'able VT became surprising for developers etc.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">czw., 11 lip 2024 o 17:33 Ron Pressler <<a href="mailto:ron.pressler@oracle.com" target="_blank">ron.pressler@oracle.com</a>> napisał(a):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
> On 11 Jul 2024, at 12:38, Michal Domagala <<a href="mailto:outsider404@gmail.com" target="_blank">outsider404@gmail.com</a>> wrote:<br>
> <br>
> Regardless of the main issue, I understand that VT GC is problematic. JEP 444 is modified and GC paragraph is removed.<br>
> VT created by Thread API is strongly referenced to avoid finalization problems<br>
<br>
The reason for keeping a reference to virtual threads by default is not finalization but observability. The cost of holding a strong reference is near zero because we can rely on the same strong references in constructs such as Executors and StructuredTaskScope, while adding WeakReferences would come at a cost.<br>
<br>
> <br>
> What does it mean? I do not understand this paragraph. Will G1 clean VT or not?<br>
<br>
In most cases it will not because we keep a reference to the thread for observability unless you turn that off with a system property.<br>
<br>
— Ron</blockquote></div></div></div></div></div>