java Web 应用程序似乎启动了一个名为 [22] 的线程,但未能将其停止。这很可能造成内存泄漏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5905252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:22:10  来源:igfitidea点击:

A web application appears to have started a thread named [22] but has failed to stop it. This is very likely to create a memory leak

javamultithreadingservlets

提问by rl99

I have a web application with Servlets in the back end deployed over tomcat. The application is simple java application.

我有一个 Web 应用程序,在后端通过 tomcat 部署了 Servlet。该应用程序是简单的java应用程序。

I see this error frequently in the server logs: SEVERE: A web application appears to have started a thread named [22] but has failed to stop it. This is very likely to create a memory leak.

我经常在服务器日志中看到此错误:严重:Web 应用程序似乎已启动名为 [22] 的线程,但未能将其停止。这很可能造成内存泄漏。

Are there any potential reasons which might be causing it?

是否有任何可能导致它的潜在原因?

采纳答案by duffymo

I'd use visualvm 1.3.2and see what threads are being created. Be sure to add all the plug-ins.

我会使用visualvm 1.3.2并查看正在创建哪些线程。请务必添加所有插件。

If it's not being done by your code you won't have much control over it.

如果它不是由您的代码完成的,您将无法对其进行太多控制。

You also don't know if the message is a red herring or not. Load test your code over a period of time and measure what happens.

您也不知道该消息是否是红鲱鱼。在一段时间内对您的代码进行负载测试并测量会发生什么。

回答by Vipin

I faced similar situation recently Resolved in below steps

我最近遇到了类似的情况,按以下步骤解决

  1. I took Thread dump. ( using Kill -QUIT pid )
  2. Found the Runnable/Thread class form dump
  3. Then i put a debug point in run method and started application in debug mode.
  4. Got the code which starts My Thread and I observed it was not stopped while stoping application.
  5. Introduced code to stop the thread in contextDestroyed method of AppContextListener (This is My application class which extends ServletContextListener ) as this method will be called when i stop tomcat.
  1. 我拿了线程转储。(使用 Kill -QUIT pid )
  2. 找到 Runnable/Thread 类表单转储
  3. 然后我在 run 方法中放置了一个调试点并以调试模式启动应用程序。
  4. 得到了启动我的线程的代码,我观察到它在停止应用程序时没有停止。
  5. 引入了在 AppContextListener 的 contextDestroyed 方法中停止线程的代码(这是我的应用程序类,它扩展了 ServletContextListener ),因为当我停止 tomcat 时将调用此方法。

If you set Thread as Dameon Thread it is not going to help , you can visit explanation.

如果您将 Thread 设置为 Dameon Thread 则无济于事,您可以访问解释

回答by Guy

Tomcat waits for all the application's threads (user threads not daemon threads) to stop before it goes down, I guess that in your case this specific thread is a user thread and therefore tomcat generated this error. I suggest you to change this thread to daemon (assuming this one is yours)

Tomcat 等待所有应用程序的线程(用户线程而不是守护线程)在它关闭之前停止,我猜在你的情况下这个特定线程是一个用户线程,因此 tomcat 生成了这个错误。我建议您将此线程更改为守护进程(假设这是您的)