-XX:OnOutOfMemoryError="kill -9 %p" Problem

41,467

Solution 1

In Java version 8u92 the VM arguments

  • -XX:+ExitOnOutOfMemoryError
  • -XX:+CrashOnOutOfMemoryError

were added, see the release notes.

ExitOnOutOfMemoryError
When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors.

CrashOnOutOfMemoryError
If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files.

Enhancement Request: JDK-8138745 (parameter naming is wrong though JDK-8154713, ExitOnOutOfMemoryError instead of ExitOnOutOfMemory)

Solution 2

The single quote version should work fine in jetty >9.0.4 now.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=408904

Solution 3

Running as a hadoop option I run in to the same issues. This was the answer:

-XX:OnOutOfMemoryError='kill -9 %p'

Here is stdout on OOM:

#
# java.lang.OutOfMemoryError: Java heap space
# -XX:OnOutOfMemoryError="kill -9 %p"
#   Executing /bin/sh -c "kill -9 11902"...

I also tried:

-XX:OnOutOfMemoryError='"kill -9 %p"'

It started, but on OOM it

# java.lang.OutOfMemoryError: Java heap space
# -XX:OnOutOfMemoryError="kill' '-9' '%p"
#   Executing /bin/sh -c "kill' '-9' '1164"...

But STDERR has: sh: kill -9 1164: command not found

These Wouldn't even start:

'-XX:OnOutOfMemoryError=kill -9 %p'
"-XX:OnOutOfMemoryError=kill -9 %p"
-XX:OnOutOfMemoryError="kill -9 %p"

Solution 4

I've run through this problem pretty recently. I have solved it by setting the option into the JAVA_TOOL_OPTIONS environment variable. This variable is documented by Oracle and you must export this variable on you shell commands and the JVM will append it to the arguments.

Solution 5

I believe you need to quote the whole option, like this:

  "-XX:OnOutOfMemoryError=kill -9 %p"
Share:
41,467
Patrick
Author by

Patrick

Updated on March 04, 2021

Comments

  • Patrick
    Patrick over 3 years

    I have an issue with trying to pass the -XX:OnOutOfMemoryError="kill -9 %p" command into my jvm args.

    I am using Jetty7, and have this within the start.ini file. On start up it give me the error below. This is with jre /jre1.6.0_03l64

    Starting Jetty: STARTED Jetty Tue Apr 26 09:54:26 EDT 2011
    Unrecognized option: -9
    Could not create the Java virtual machine.

    The start.ini file is as below.

    #===========================================================
    # If the arguements in this file include JVM arguments
    # (eg -Xmx512m) or JVM System properties (eg com.sun.???),
    # then these will not take affect unless the --exec
    # parameter is included or if the output from --dry-run
    # is executed like:
    #   eval $(java -jar start.jar --dry-run)
    #
    # Below are some recommended options for Sun's JRE
    #-----------------------------------------------------------
      --exec
    # -Dcom.sun.management.jmxremote
      -Xmx4096m
      -Xmn512m
      -DLABEL=PROD_APP
      -verbose:gc
      -Xloggc:/export/opt/prod_app/logs/gc.log
      -XX:OnOutOfMemoryError="kill -9 %p"
    # -XX:+PrintGCDateStamps
      -XX:+PrintGCTimeStamps
      -XX:+PrintGCDetails
      -XX:+PrintTenuringDistribution
    # -XX:+PrintCommandLineFlags
    # -XX:+DisableExplicitGC
    # -XX:+UseConcMarkSweepGC
    # -XX:ParallelCMSThreads=2
    # -XX:+CMSClassUnloadingEnabled
    # -XX:+UseCMSCompactAtFullCollection
    # -XX:CMSInitiatingOccupancyFraction=80
    

    Commenting the line out jetty will start fine with no issue. However we really need to add this arg due to memory leak with the system to prevent further damage if our process falls over.

    Would anyone have any idea what I am doing wrong here or how I can fix this?

    • Wim Deblauwe
      Wim Deblauwe about 13 years
      I had problems with something similar as well but it was in my own batch/bash files. Maybe jetty puts his own quotes around everything in the .ini file. You might try with single quotes (') instead?
    • eee
      eee about 13 years
      I wonder why it doesn't work... It says -XX:OnOutOfMemoryError="<cmd args>;<cmd args>" Run user-defined commands when an OutOfMemoryError is first thrown. (Introduced in 1.4.2 update 12, 6) oracle.com/technetwork/java/javase/tech/…
  • Patrick
    Patrick about 13 years
    If i put quote around the entire option then id does not appear in the actual process args, i would have expected to see it in here. Although it does start up which is positive. //1526 1506 99 04:51 pts/2 00:00:05 /usr/java/jre1.6.0_03l64/bin/java -Xmx4096m -Xmn512m -verbose:gc -Xloggc:/export/opt/atdirect_dev/logs/gc.log -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -Djetty.home=/export/opt/PROD_APP –Dprod_app.connection-client.cfg=/export/opt/PROD_APP/lib/PR‌​OD/prod.connection-c‌​lient.cfg -DLABEL=PROD_APP
  • jesse mcconnell
    jesse mcconnell about 11 years
    I opened a bug for this here: bugs.eclipse.org/bugs/show_bug.cgi?id=408904 This should have been reported as a bug years ago and we could have gotten it addressed earlier.
  • Joakim Erdfelt
    Joakim Erdfelt about 11 years
    The error sh: kill -9 1164: command not found is telling you what is important. That the executable passed to sh is kill -9 1164 (note, this is name of the executable that it is trying to run). Proof: if you type something like $ blargfish -9 1164 on your command line right now, you get blarghfish: command not found. Notice that the command line arguments are not present in the error message.