问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/53274982/how-java-visualvm-shows-threads-that-are-not-running-finished