简体   繁体   English

垃圾收集线程太多

[英]Too many Garbage collection threads

I am developing an software with java, it creates a thread upon receiving an event (from sensors). 我正在使用java开发一个软件,它在接收事件(来自传感器)时创建一个线程。 the time-to-live of these threads are very small (< 1 second) 这些线程的生存时间非常短(<1秒)
The sensor sends maximal 10 events/minute. 传感器发送 最多10个事件/分钟。 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 @_@) 我看看eclipse调试器并发现有很多线程,其中大多数是"Thread[garbage collected]" (大约800个线程@ _ @)

I don't know if that bug is caused by dynamic-creating-threads in my code or other bugs? 我不知道该bug是否是由我的代码中的动态创建线程或其他错误引起的?

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!). 我已经用时间戳记录了所有传感器的事件,并且它创建了大约1200 events/minute某些点(该死的!)。
I also wrote a small java program which creates as many threads as posible. 我还写了一个小的java程序,它创建了与posible一样多的线程。 At ~4100th thread (well, wooden computer) , jvm crashes. ~4100th thread (好吧,木制电脑),jvm崩溃了。 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? 因此,我认为动态创建线程时可能存在(可能)罕见的情况,并导致垃圾收集线程挂起?

在此输入图像描述

Don't create a new thread for each event that is received. 不要为收到的每个事件创建新线程。 Instead, use the classes from the java.util.concurrent package. 而是使用java.util.concurrent包中的类。

Create an ExecutorService (using one of the static methods in class Executors ) and submit jobs for handling events to the ExecutorService . 创建一个ExecutorService (使用类Executors一个静态方法)并将处理事件的作业提交给ExecutorService The ExecutorService is a thread pool that manages the threads for you. ExecutorService是一个线程池,可以为您管理线程。

I had a similar problem in NetBeans. 我在NetBeans中遇到了类似的问题。 After a while, the program would hang with lots (maybe 500) of suspended threads. 过了一会儿,程序会挂起很多(可能是500个)挂起的线程。 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. 到目前为止,我的经验是JVM很好地处理了大量快速启动和停止的线程,但是NetBeans调试器(现在的几个版本,6个版本之一)没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM