4

We have a service deployed on Tomcat 8. It usually runs fine, but it is observed that tomcat abruptly crashes sometimes. Today when it did, I found following logs present:

 SEVERE org.apache.tomcat.util.net.NioEndpoint$Acceptor.run 
       java.lang.OutOfMemoryError: Java Heap Space

I figured that, this occurred when the service tried to hit a POST request. The code for the same is as follows:

  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();

What could be the reason for tomcats' crash? Is it because of any of the following reasons?

  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-#######"

I am stuck at the moment and worried about the thing that, this scenario might occur again. I need to find the root cause for the same. Any help or advice regarding the same would be really useful.

1 Answer 1

2

Add the following to your jvm arguments:

-Xms512m -Xmx2048m -XX:+HeapDumpOnOutOfMemoryError

The next time an error occurs, you should see a *.hprof file in your startup folder. Use a profiler to open that, my favorites are yjp and mat. Either will show you the top contenders for heap in the application as well as number of threads and resource usage.

Hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

Yes. Will do that. But the thing is, this scenario is not reproducible and not sure when it will happen again. Till then, do you think my assumptions are right?
my Xms and Xmx both have been set to 2GB
@neerajdorle OOM is the culmination of possibly various memory leaks within the application. You didn't say whether that's client side or server (Tomcat) side code that you have posted, so it's difficult to answer your questions conclusively. But over time if Java is unable to recover memory that has been allocated, OOM may be thrown, but it isn't necessarily caused by the last executed stack, it simply can't go any further because other stuff is filling up the heap.
I forgot to mention resource leaks. You should also surround the HttpsUrlConnection with a try-with-resources statement
@ThomasTimbul yes that is right. The code that I have pasted is from my service which is running on Tomcat and it tries to access some other webservice ....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.