java - Tomcat 在尝试发送 HTTP 请求时因 java.lang.OutOfMemory 错误而崩溃

标签 java out-of-memory tomcat8

我们在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 崩溃的原因可能是什么?是否因为以下原因?

  1. Most of the resources like outputstream and inputstream are not closed?
  2. Also the the HttpsURLConnectioncon is not closed/ disconnected?
  3. 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.
  4. I also saw that the OME was thrown by ByteArray or something like that. Not sure about that. Just remember seeing something.
  5. Along with the logs, following exception was also seen: Exception in thread "AsyncFileHandlerWriter-#######"

我现在卡住了,担心这种情况可能会再次发生。我需要找到相同的根本原因。任何关于相同的帮助或建议都会非常有用。

最佳答案

将以下内容添加到您的 jvm 参数中:

-Xms512m -Xmx2048m -XX:+HeapDumpOnOutOfMemoryError

下次发生错误时,您应该会在启动文件夹中看到 *.hprof 文件。使用分析器打开它,我最喜欢的是 yjpmat .两者都会向您显示应用程序中堆的主要竞争者以及线程数和资源使用情况。

希望这对您有所帮助。

关于java - Tomcat 在尝试发送 HTTP 请求时因 java.lang.OutOfMemory 错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272964/

相关文章:

java - BigInteger 类中的无用变量,为什么?

java - 除了 Class 对象之外,Permgen Space (sun 1.6 VM) 中还存储了什么?

java - 为什么heroku中部署的tomcat中的PUT方法的请求参数总是NULL

tomcat - 增加 Eden 空间堆大小 tomcat

java - 为什么 Selenium 在点 (.) 字符后面附加一个反斜杠,对于其中包含 (.) 的 ID 属性的值产生 (\.)

java - 用Java解析JSON数组

java - 关于使用 Oracle 10g 进行后续锁定的 Hibernate 警告

.net - 集合内存不足

c# - 如何在 Windows Phone 8 中释放图像缓存/内存?

java - 为什么 Tomcat8.exe 启动的 JAVA_HOME 与 Windows 环境变量中设置的 JAVA_HOME 不同?