在Tomcat中使用net.http.HttpClient导致内存泄漏 [英] Using net.http.HttpClient in Tomcat causes memory leak
问题描述
我在基于 Servlet 的 Web 应用程序中使用 Java 的新(自版本 11 起)HttpClient
:
I'm using the Java's new (since version 11) HttpClient
within a Servlet based web application:
private static final HttpClient HTTP_CLIENT =
.connectTimeout(Duration.ofSeconds(5))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
...
public void httpPostAsyncToEndpoint(WebEndpoint endpoint, Map<String,String> params) {
HttpRequest req = buildRequest(endpoint, params);
CompletableFuture<HttpResponse<String>> future = HTTP_CLIENT.sendAsync(req, HttpResponse.BodyHandlers.ofString());
future.thenAccept((HttpResponse<String> res) -> {
if (res.statusCode() >= 400) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("{} HTTP response returned from endpoint {}", endpoint, res.statusCode());
}
}
}).exceptionally(ex -> {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Could not audit event using endpoint {}", endpoint, ex);
}
return null;
});
}
一切正常,除了在 Tomcat 上重新启动 Web 应用程序时会产生以下警告:
Everything is working great, except that when the web application is restarted on Tomcat the following warning is produced:
14-Aug-2020 09:21:16.996 WARNING [http-nio-8080-exec-18] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp] appears to have started a thread named [HttpClient-3-SelectorManager] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.base/sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
java.base/sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:357)
java.base/sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:182)
java.base/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:124)
java.base/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:136)
java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:867)
我怎样才能防止这种情况发生?我尝试使用自定义 ThreadFactory
只返回守护线程:
How can I prevent this? I've attempted to use a custom ThreadFactory
which returns only daemon threads:
HttpClient.newBuilder()
.executor(Executors.newSingleThreadExecutor((Runnable r) -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}))
.connectTimeout(Duration.ofSeconds(5))
.followRedirects(HttpClient.Redirect.NORMAL).build();
但警告仍然存在.
我在 Tomcat 9 上使用 OpenJDK 11.0.7.
I'm using OpenJDK 11.0.7 on Tomcat 9.
推荐答案
只要对 HttpClient
的引用还活着 - 或者只要由客户端仍在进行中.线程可能需要几秒钟才能检测到 HttpClient
不再被引用.所以我不相信你所拥有的是真正的泄漏 - 除非持有对 HttpClient
的静态引用的类由于其他原因留在内存中.
The selector manager thread will remain alive as long as the reference to the HttpClient
is alive - or as long as an operation initiated by the client is still in progress. It may take a couple of seconds for the thread to detect that the HttpClient
is no longer referenced. So I don't believe that what you have is an actual leak - unless the class that held the static reference to the HttpClient
stays pinned in memory for other reasons.
这篇关于在Tomcat中使用net.http.HttpClient导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!