垃圾收集线程太多 [英] Too many Garbage collection threads

查看:147
本文介绍了垃圾收集线程太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java开发一个软件,它会在接收到一个事件(来自传感器)时创建一个线程。这些线程的生存时间非常短(<1秒)

传感器每分钟发送一次最大10次事件。
这个应用程序工作正常大部分时间。但有一段时间它挂起。

我看看eclipse调试器,发现有很多线程,其中大部分是Thread [garbage collected](大约800个线程@ _ @)

我不知道该错误是否是由我的代码或其他错误中的动态创建线程引起的?



编辑:

这个问题确实是由于创建了太多的线程而导致的。我已经用时间戳记录了所有传感器的事件,并且创建了一些关于 1200事件/分钟(该死的!)的点。

我还写了一个一个小型的java程序,可以创建尽可能多的线程。在〜4100th线程(以及木制电脑),jvm崩溃。它不会像我的应用那样挂起: - ?.

因此,我想在创建线程时有(可能)极少情况,并且会导致垃圾回收线程挂起?

解决方案

不要为收到的每个事件创建一个新线程。相反,使用 java.util.concurrent 包中的类。



创建一个 ExecutorService (使用类 Executors 中的静态方法之一),并将处理事件的作业提交给 ExecutorService ExecutorService 是为您管理线程的线程池。


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?

解决方案

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.

这篇关于垃圾收集线程太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆