我们在Tomcat 8上部署了一个服务,平时运行正常,但是观察到tomcat有时会突然崩溃。今天,当它发生时,我发现存在以下日志:
SEVERE org.apache.tomcat.util.net.NioEndpoint$Acceptor.run
java.lang.OutOfMemoryError: Java Heap Space
我想,这发生在服务试图命中 POST 请求时。其代码如下:
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
tomcats 崩溃的原因可能是什么?是否因为以下原因?
- Most of the resources like
outputstream and inputstream
are not closed?- Also the the HttpsURLConnection
con
is not closed/ disconnected?- If so, then why it doesn't happen every time. In fact, this is observed for the first time in more than couple of months.
- I also saw that the OME was thrown by ByteArray or something like that. Not sure about that. Just remember seeing something.
- Along with the logs, following exception was also seen: Exception in thread "AsyncFileHandlerWriter-#######"
我现在卡住了,担心这种情况可能会再次发生。我需要找到相同的根本原因。任何关于相同的帮助或建议都会非常有用。
最佳答案
将以下内容添加到您的 jvm 参数中:
-Xms512m -Xmx2048m -XX:+HeapDumpOnOutOfMemoryError
下次发生错误时,您应该会在启动文件夹中看到 *.hprof 文件。使用分析器打开它,我最喜欢的是 yjp和 mat .两者都会向您显示应用程序中堆的主要竞争者以及线程数和资源使用情况。
希望这对您有所帮助。
关于java - Tomcat 在尝试发送 HTTP 请求时因 java.lang.OutOfMemory 错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272964/