我们将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.
Run Code Online (Sandbox Code Playgroud)
我们可能在某处做一些天真的事情,因为在这种情况下我们无法在网络上的任何地方找到任何帮助。不过,我们希望这里的人可以告诉我们我们要去哪里错了。
这是我们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;
}
}
Run Code Online (Sandbox Code Playgroud)
我看到了类似的问题。如果你看这里,就会发现一个静态 ThreadLocal 缓存已被填满,无法删除它:
private static final ThreadLocal<Map<Schema, Map<Schema, ResolvingDecoder>>> RESOLVER_CACHE = ThreadLocal
.withInitial(WeakIdentityHashMap::new);
Run Code Online (Sandbox Code Playgroud)
就我而言,每个请求都有不同的架构,因此我的 Spring Boot 服务最终会耗尽内存并终止。
这也是类加载器泄漏的一个示例: ThreadLocal & Memory Leak
我可以在这里看到 JIRA 请求以进行改进: https://issues.apache.org/jira/browse/AVRO-1595
更新:我能够解决内存泄漏的问题。有一个 ExecutorService 正在重用线程。现在我关闭导致线程完成的服务。这允许对 ThreadLocal 内存进行 GC。
正如有人指出的那样,RESOLVER_CACHE 使用 WeakIdentityHashMap 应该允许缓存被 GC,但这对我来说并没有发生。
| 归档时间: |
|
| 查看次数: |
422 次 |
| 最近记录: |