Apache Tomcat 请求线程
我们有一个应用程序泄漏了一些内存,这是轻描淡写的。
我正在使用jvisualvm
来尝试找出导致问题的原因。
我看到以名称开头的线程数增长了不少: http-8080- example: http:8080-42
我的第一个猜测是,每个线程都是来自客户端的请求命中,因为每个客户端请求都在其自己的线程中处理。
我的问题是这些线程已经运行了很长一段时间(到目前为止 10 分钟)。
我的问题是:
我的假设正确吗? 如果是这样,为什么线程运行这么长时间?它肯定不能仍然忙于满足客户的请求吗?
We have an application which leaks a bit of memory, a bit being an understatement.
I am using jvisualvm
to try and find what is causing the problem.
I see the thread count grow quite a bit on threads starting with the name: http-8080- example: http:8080-42
My first guess is that each of those threads is a request hit from the client, as each client request is handled in its own thread.
My my problem is that those threads have been running for long periods of time (Thus far 10mins).
My question is this:
Is my assumption correct?
If so, why is it that the Threads run for such a long time? Surely it can't still be busy serving the clients request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Tomcat 总是有许多等待的 HTTP 线程,例如,如果我们查看默认连接器设置:
我们可以看到应该始终有至少 25 个活动线程,但正在等待连接(最多达到 maxThreads 限制)。这是由 min 和 maxSpareThreads 属性控制的。
JVisual VM 声明线程正在等待或锁定资源等是什么?
Tomcat always has a number of waiting HTTP threads, for example if we look at the default connector setting:
We can see that there should always be at least 25 threads live, but waiting for connections (up to the maxThreads limit). This is controlled by the min and maxSpareThreads attributes.
What does JVisual VM state that the thread is doing waiting or locked on a resource etc etc?
一般来说,应用服务器会预先创建一些线程。应用程序服务器不仅会创建它们,还会保留线程。这称为线程池。服务器将接受请求并将其分派到线程,当该请求完成时,服务器将向该线程分派新请求。
线程创建开销相当昂贵,因此处理许多请求可以从共享线程中受益匪浅。为了回答您的问题,调度由服务器创建的线程(假设没有发生严重的运行时错误)将在服务器的生命周期内有效。
至于您所看到的,如果您看到许多线程正在启动,那么应用程序的其他部分可能会分叉线程,这是一个完全独立的问题。
重要的是要知道您的 tomcat 服务器不应该为每个请求创建新线程(同样一般来说),它应该重用线程。
Generally speaking, application servers will pre create a number of threads. Not only will the app server create them, but it will keep the threads around. This is known as a thread pool. The server will take a request and dispatch it to a thread, and when that request completes, the server will dispatch a new request to that thread.
Thread creation overhead is rather expensive so handling many requests benefits greatly from sharing threads. To answer your question, dispatching threads created by the server (assuming no serious runtime errors occur) will live for the lifetime of the server.
As for what you are seeing, if you see many many threads being started, then some other part of the application can be forking threads in which is a completely separate issue.
Its important to know that your tomcat server should not be creating new threads for each request (again generally speaking) it should reusing threads.
检查 tomcat 连接器配置。注意
maxThreads
和其他线程池配置。一个常见的错误是只增加 maxThreads 而不实际“调整”。如果您配置了不必要的大池,则会导致大量空闲线程。这不会有什么好处。尽管很明显,但仅供记录,TIMED_WAITING 线程将超时,而 WAITING 线程将等待
notify()
或notifyAll()
。Check the tomcat connector configurations. Pay attention to
maxThreads
and other thread pool configuration. A common mistake is to just increasemaxThreads
without actually "Tuning". If you configure an unnecessarily large pool, it will lead to lots of idle threads. This will do no good.Even though it's obvious, just for the record, TIMED_WAITING threads will time out, and WAITING threads will just lay around for a
notify()
or anotifyAll()
.