5

我们使用 Apache Avro 作为 Python 应用程序和我们在 Tomcat 服务中运行的一些第三方 Java 库之间的 JSON 接口。我们决定简单地扩展 org.apache.avro.ipc.ResponderServlet 类来实现我们自己的 servlet。servlet 非常简单,因为它在构造函数中实例化了 ResponderServlet 超类,并重写了 init() 和 destroy() 方法来为我们在 servlet 中运行的第三方库做一些内务处理。

然而,当 Tomcat 取消部署我们的 web 应用程序时,我们会看到许多严重错误警告,警告 ThreadLocal 相关的内存泄漏。

SEVERE: The web application [/hotwire] created a ThreadLocal with key of type [org.apache.avro.Schema$3] (value [org.apache.avro.Schema$3@4464784f]) and a value of type [java.lang.Boolean] (value [true]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jan 24, 2013 2:19:36 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/hotwire] created a ThreadLocal with key of type [org.apache.avro.generic.GenericDatumReader$1] (value [org.apache.avro.generic.GenericDatumReader$1@2016ad9d]) and a value of type [org.apache.avro.util.WeakIdentityHashMap] (value [org.apache.avro.util.WeakIdentityHashMap@30e02ee0]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

我们可能在某处做一些幼稚的事情,因为我们无法在网络上的任何地方为这种情况找到任何帮助。尽管如此,我们希望这里有人能告诉我们哪里出了问题。

这是对我们的 servlet 的一瞥。

public class HotWire extends ResponderServlet{
    public HotWire() throws IOException
    {
              super(new SpecificResponder(Engine.class, new EngineImpl()));
    }

    @Override
        public void init() {
        try {
         super.init();
                 try {
                      init_engine();                
                      } catch (EngineInitException e) {
                      e.printStackTrace();
                      }
            } catch (ServletException e) {
                 e.printStackTrace();
            }
        }

        @Override
        public void destroy() {
            super.destroy();
            shutdown_engine();
        }

        public static class EngineImpl implements EngineInterface  {
            public Boolean create(Parameters message) {
                Boolean status = null;
                try {
                    status = engine.create_object(message);
                } catch (SemanticException | IOException e) {
                    e.printStackTrace();
                }
                return status;
            }

}
4

1 回答 1

1

我看到了类似的问题。如果你看这里有一个静态 ThreadLocal 缓存被填满,无法删除它:

https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java#L106

  private static final ThreadLocal<Map<Schema, Map<Schema, ResolvingDecoder>>> RESOLVER_CACHE = ThreadLocal
      .withInitial(WeakIdentityHashMap::new);

在我的例子中,每个请求都有不同的模式,所以我的 Spring Boot 服务最终会耗尽内存并死掉。

这也是类加载器泄漏的一个例子: ThreadLocal & Memory Leak

我可以在这里看到 JIRA 请求进行改进https ://issues.apache.org/jira/browse/AVRO-1595

更新:我能够解决内存泄漏的问题。有一个 ExecutorService 正在重用线程。现在我关闭了导致线程完成的服务。这允许对 ThreadLocal 内存进行 GC。

正如有人指出的那样,RESOLVER_CACHE 使用了 WeakIdentityHashMap,它应该允许缓存被 GC 处理,但这对我来说并没有发生。

于 2019-04-23T22:21:46.020 回答