Tomcat HttpURLConnection 上的内存泄漏警告
Tomcat memory leak warning on HttpURLConnection
我在 Tomcat 8.5 中有以下警告,我不确定是否可以忽略
WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [AppName] appears to have started a thread named [pool-20-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
java.net.SocketInputStream.read(SocketInputStream.java:171)
java.net.SocketInputStream.read(SocketInputStream.java:141)
sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
sun.security.ssl.InputRecord.read(InputRecord.java:503)
sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
它发生在 connect
上,代码如下:
URL url = new URL(MY_URL);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setFixedLengthStreamingMode(out.length);
http.setRequestProperty("Content-Type", "application/json");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
我应该添加一个 finally 并关闭 InputStream
:
http.getInputStream().close();
或者此代码应该是 upgraded/converted 以使用 URIBuilder
和 HttpHost
?
编辑
使用 Spring 的 @Scheduled
每 X 秒执行一次代码
您正在 Tomcat 中使用 @Scheduled
生成线程。您必须确保这些线程将在 ServletContext
被销毁时完成,例如WAR 正在取消部署,Tomcat 会就此向您发出警告。 HTTP 请求代码不相关,因为 URLConnection
不会启动新线程来执行请求。
让 Tomcat 开心的一种方法是使用守护线程,如 this answer 中所述。这可以通过自定义 taskScheduler
bean:
来完成
@Configuration
@EnableScheduling
public class TaskConfiguration {
@Bean(destroyMethod = "shutdown")
public Executor taskScheduler() {
return Executors.Executors.newFixedThreadPool(4,
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
});
}
}
我在 Tomcat 8.5 中有以下警告,我不确定是否可以忽略
WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [AppName] appears to have started a thread named [pool-20-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
java.net.SocketInputStream.read(SocketInputStream.java:171)
java.net.SocketInputStream.read(SocketInputStream.java:141)
sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
sun.security.ssl.InputRecord.read(InputRecord.java:503)
sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
它发生在 connect
上,代码如下:
URL url = new URL(MY_URL);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setFixedLengthStreamingMode(out.length);
http.setRequestProperty("Content-Type", "application/json");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
我应该添加一个 finally 并关闭 InputStream
:
http.getInputStream().close();
或者此代码应该是 upgraded/converted 以使用 URIBuilder
和 HttpHost
?
编辑
使用 Spring 的 @Scheduled
您正在 Tomcat 中使用 @Scheduled
生成线程。您必须确保这些线程将在 ServletContext
被销毁时完成,例如WAR 正在取消部署,Tomcat 会就此向您发出警告。 HTTP 请求代码不相关,因为 URLConnection
不会启动新线程来执行请求。
让 Tomcat 开心的一种方法是使用守护线程,如 this answer 中所述。这可以通过自定义 taskScheduler
bean:
@Configuration
@EnableScheduling
public class TaskConfiguration {
@Bean(destroyMethod = "shutdown")
public Executor taskScheduler() {
return Executors.Executors.newFixedThreadPool(4,
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
});
}
}