riccardo.cardin
riccardo.cardin

Reputation: 8353

Spring REST Service does not shutdown after an OutOfMemoryError

I am experiencing what I consider a strange behaviour for a Java application. I am using Spring Boot 1.4.1 to develop a REST Service. Due to a bug in my code, an invocation to the service results in an OutOfMemoryError.

Surprisingly, the service responds to the request that generates the error with the following message:

{
  "timestamp": 1487862480405,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.OutOfMemoryError",
  "message": "No message available",
  "path": "/entity/exportCsv"
}

So far so good. What is more surprisingly is that the REST service does not shutdown after experiencing the Error. Someone said that after an Error the best thing to do is to properly log it and shutdown everything. The presence of an error should mean that the system is in an unrecoverable state.

Why does Spring MVC adopt such a strange policy in case of Error? Is it possible to force the exit from the application (the REST service) after an Error happened?

If you want to reproduce the above use case, just use the following code.

@RestController
@RequestMapping("/entity")
class Controller {
    @RequestMapping(value = "/exportCsv", method = RequestMethod.GET)
    ResponseEntity exportCsv() {
        if (true)
            throw new OutOfMemoryError();
        return null;
    }
}

Thanks to all.

EDIT: if you do think that catching an Error is a normal way to develop Java applications, please, try to have a look to these references:

Upvotes: 7

Views: 3554

Answers (3)

Somanath Yadav
Somanath Yadav

Reputation: 1

java.lang.OutOfMemoryError won't stop the Spring Application. If you are running your application in a container then health-check hook would let container manager know that your application is not in right condition so it would automatically dispose the current pod and would create fresh one.

You should always give JAVA_TOOL_OPTIONS as environment variable to use the POD memory effectively. By default your Java Springboot app will use just 25% of maximum memory.

e.g. If you allocate 4 GB memory to your POD then your Java Springboot application will use max 1 GB of RAM and in turn you will not use the allocated resources properly. Always set JAVA_TOOL_OPTIONS environment variable for memory allocations.

Upvotes: 0

stuchl4n3k
stuchl4n3k

Reputation: 598

I would say that in the world of web apps this is not suprising at all.

Imagine a RestController as a web page. If it contains an error, the web server nor the application that generates the page is expected to stop working. This error is usually a local or temporary problem related to your request. In these situations the web server should instead respond with HTTP status 500.

Spring even has an built-in error handling feature that lets you to handle each specific error differently. See this article by Paul Chapman for more info.

UPDATE: Regarding your inquiry about the OOM error handling: I think you missed how memory management and object allocation in JVM works. See Understand the OutOfMemoryError Exception.

Upvotes: 1

Sergio Lema
Sergio Lema

Reputation: 1629

When running your application, you can specify the behavior of your application when an error like this happens with the following arguments:

  • -XX:+HeapDumpOnOutOfMemoryError : this will create a dump which can be analysed afterwards. The dump will be located at the location given at -XX:HeapDumpPath=some_path.
  • -XX:OnOutOfMemoryError=path_to_some_script.sh : this will run a script (it must be runnable by the same user which runs the application) when the application returns an error as an OutOfMemory
  • -XX:OnError=path_to_some_script.sh : same as before, but for more generic exceptions.

Reference: http://www.oracle.com/technetwork/java/javase/clopts-139448.html

Upvotes: 5

Related Questions