How to restart java app managed by systemd on OutOfMemory errors

Like Fildor say I suggest you fix the memory problems.

After that a possible solution is:

If you are using Java prior 8u92 you can add to the JVM the following argument:

java -jar <jar-name> -XX:OnOutOfMemoryError="kill -9 %p"

in Java version 8u92 or higher you can use -XX:+CrashOnOutOfMemory or -XX:+ExitOnOutOfMemoryError

Then configure your service to restart on crash:

Restart=on-failure

or

Restart=always