Script when Java Heap space error is raised

I want to run server restart script when my Java Play Application throws heap space error.
Excat error is:- java.lang.OutOfMemoryError: Java heap space

Also in config file:- akka {
jvm-exit-on-fatal-error = on
}
This parameter is set.
When the error is raised , My App downs, and I have to manually restart it.
I want to know how can I restart the app again, Is play provides any config options where I can set path of my shell/batch script file which will be executed(to restart server) when out of memory error is raised?

Hello chaltanya,
what you’re suggesting here is inherently unsafe.

A JVM process that signals exhaustion of the heap would imply that you can’t be sure anymore if the app will be able to continue working correctly. That’s the reason why akka provides the flag to automatically stop the jvm on such kind of errors…

I think you could envision an error handler (and I don’t know where you would put it to guarantee catching such RuntimeErrors), to spawn a separate process that will take place of the running one, and then close the currently failing JVM. But such operation might fail itself, because you’re not sure if the process is anymore in a stable condition.

That said, the general wisdom nowadays is that you want your JVM process to be monitored by an external orchestration system (docker, kubernetes, mesos, …) that will verifiy if your application is running correctly with a periodical heartbeat signal. Should a sufficient number of heartbeats fail to respond, the orchestrator would deduce that the process is gone and will decide if and how to restart it.

1 Like

If you don’t want the complexity of a system like Kubernetes, there are also many simple and mature process monitoring tools available that have been used on servers for years to solve this kind of problem. Your operating system might have one built in, such as systemd or upstart, or you can install another one such as monit.

Here are some resources:

2 Likes

Thanks a lot.

Yeah this is easy, I will work on it.Thanks