Too many Garbage collection threads

人盡茶涼 提交于 2020-01-14 17:56:39

问题


I am developing an software with java, it creates a thread upon receiving an event (from sensors). the time-to-live of these threads are very small (< 1 second)
The sensor sends maximal 10 events/minute. This app works fine for the most of time. but some time it hangs.
I look at eclipse debugger and find out that there are to many threads and most of them are "Thread[garbage collected]" (about 800 threads @_@)

I don't know if that bug is caused by dynamic-creating-threads in my code or other bugs?

EDIT:
The problem is indeed caused by creating too many threads. I have logged all sensor's events with timestamp and there are some points it creates about 1200 events/minute (damn!).
I also wrote a small java program which creates as many threads as posible. At ~4100th thread (well, wooden computer) , jvm crashes. It does not hangs like my app does :-?.
Therefore I suppose that there are (maybe) rare conditions while creating threads dynamiccally and it causes the garbage collection threads hang?


回答1:


Don't create a new thread for each event that is received. Instead, use the classes from the java.util.concurrent package.

Create an ExecutorService (using one of the static methods in class Executors) and submit jobs for handling events to the ExecutorService. The ExecutorService is a thread pool that manages the threads for you.




回答2:


I had a similar problem in NetBeans. After a while, the program would hang with lots (maybe 500) of suspended threads. However, when run outside the debugger it worked just fine. I don't think I ever got more than a couple hundred threads going at once running when running outside, but the program did tend to start them at a furious rate. My suspicion was that the debugger never shut down a thread and could only handle so many.

My experience so far is that the JVM handles vast numbers of rapidly starting and stopping threads very well, but that the NetBeans debugger (several versions ago now, one of the 6's) did not.



来源:https://stackoverflow.com/questions/17808648/too-many-garbage-collection-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!