Tomcat Guice/JDBC Memory Leak

后端 未结 9 2038
长发绾君心
长发绾君心 2020-11-27 12:14

I\'m experiencing a memory leak due to orphaned threads in Tomcat. Particularly, it seems that Guice and the JDBC driver are not closing threads.

Aug 8, 2012         


        
9条回答
  •  悲&欢浪女
    2020-11-27 12:58

    I went a step further from Oso,improved the code above in two points:

    1. Added the Finalizer thread to the need-to-kill check:

      for(Thread t:threadArray) {
              if(t.getName().contains("Abandoned connection cleanup thread") 
                  ||  t.getName().matches("com\\.google.*Finalizer")
                  ) {
              synchronized(t) {
                  logger.warn("Forcibly stopping thread to avoid memory leak: " + t.getName());
                  t.stop(); //don't complain, it works
              }
          }
      }
      
    2. Sleep for a little while to give threads time to stop. Without that, tomcat kept complaining.

      try {
          Thread.sleep(1000);
      } catch (InterruptedException e) {
          logger.debug(e.getMessage(), e);
      }
      

提交回复
热议问题