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?
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.