[External] : Re: Virtual thread memory leak because TrackingRootContainer keeps threads

Ron Pressler ron.pressler at oracle.com
Tue Jul 23 14:56:57 UTC 2024


> On 22 Jul 2024, at 22:51, Michal Domagala <outsider404 at gmail.com> wrote:
> 
> Thanks for the explanation.
> 
> I understand that paragraph
> 
> "Unlike platform thread stacks, virtual thread stacks are not GC roots. Thus the references they contain are not traversed in a stop-the-world pause by garbage collectors, such as G1, that perform concurrent heap scanning"
> 
> can be rewritten as
> 
> "Some GC, such as G1, marks GC roots in stop-the-world pause. Unlike platform thread stacks, virtual thread stacks are not GC roots, therefore they do not impact stop-the-world pause."
> 
> In my opinion, the current paragraph in JEP 444 requires readers to have a deep GC background. Usually, developers are not aware of GC root cost (at least I was not aware). Developers could tune the number of GC roots by changing the number of platform threads. Others, like static variables, are rather not tunable.
> But usually, the OS limits the number of platform threads much more strictly than GC performance.
> 
> To sum up, JEP 444's message is: "Do not be afraid of the G1 initial mark phase when using virtual threads". But I think most developers, like me, never heard about it. Ohers, more advanced, could also never care about it, because Oracle docs says about the initial mark phase: "This phase is piggybacked on a normal (STW) young garbage collection.". I understand this sentence as the phase is "for free".
> 
> To sum up again: when a developer like me reads that VT is not GC root, he does not see G1 profit behind. He reads: VT is GC'able. And the current state, when behavior is different, is misleading.

A virtual thread *is* GCable, just like a String, when it is not strongly referenced. However, by default virtual threads will have a strong reference for observability, but you can turn that off.

But, yes, the very notion of GC roots requires an advanced understanding of how the Java platform’s GCs work. I *think* that the very notion of GC roots is not part of the spec, but an implementation detail. No inference can be made, for any object, from whether or not it is a root, to when it will be collected. In fact, the platform’s GC make no guarantees as to when objects are collected, regardless of whether they’re roots or not. The only guarantee is that an object will not be collected if it is strongly reachable.

— Ron


More information about the loom-dev mailing list