Jeredriq Demas
Jeredriq Demas

Reputation: 771

How Java VisualVM shows threads that are not running/finished?

enter image description here

For testing I've created a thread which has just sleep in it. And I know that GC doesnt collect them for a while even if their usage is done but when you dont keep them as an object after they complete their task they should've be gone.

So for testing purposes I used Java VisualVM but this is the first time I'm using it. And I see all these timer threads lying around with 0ms but I can still see them. Is this normal? And what does this mean? If I spam thousands of them, will it slow down my app?

Upvotes: 0

Views: 1975

Answers (2)

Tomas Hurka
Tomas Hurka

Reputation: 7001

You see all that Thread- threads, because by default VisualVM will show you all the threads (including those, which were finished during monitoring session). To see just Live threads, switch View combo from All threads to Live threads. VisualVM does not keep references to all started threads, they can be garbage-collected just fine.

Upvotes: 0

Andreas
Andreas

Reputation: 159260

Once VisualVM connects to the running JVM, it will keep references to all started threads, so those Thread objects are not garbage collected when the threads stop running.

They are retained by VisualVM so you can still see the statistics collected for them.

The Thread objects will become unreachable and GC'able once you exit VisualVM, so stopping and restarting VisualVM will "clear" the list of ended threads.

Upvotes: 1

Related Questions