可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答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
)
回答2:
I believe you need to quote the whole option, like this:
"-XX:OnOutOfMemoryError=kill -9 %p"
回答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"
回答4:
The single quote version should work fine in jetty >9.0.4 now.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=408904
回答5:
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.
回答6:
I found this option in a script and wanted to find out more about it, and google brought me here. In the script in question the option is given as
-XX:OnOutOfMemoryError='"kill -9 %p"'
So the command is double quoted, and the option value is single quoted. This isn't one of the forms shown in the other answers, so maybe it will do what you want it to?
回答7:
The following works
java -classpath $CLASSPATH "-XX:OnOutOfMemoryError=touch 'worker.oome'" $JVM_ARGS $MAIN
Most of the provided answers do not work.
However.. if you want to put it in e.g. the $JVM_ARGS variable you are in for a world of pain.
回答8:
I've even tried embedding a space using %20 but that was taken literally. So, when it got a OOM, it failed saying the command (with the embedded %20) could not be found. Crazy, I know, but was worth a try... :)
Maybe we're supposed to use the actual < and > like the Sun docs?! :P I'll try it... :)
回答9:
How about instead of invoking kill
, run a shell script that invokes kill
with the pid passed as an argument (you would still have a space, but no -9 flag).
e.g. -XX:OnOutOfMemoryError='/path/killdash9.sh %p'
If you still can't have the space, maybe try having the shell script find the pid associated with that Jetty instance? Quite a hack, but it might work.
回答10:
In a recent release of Java 8 (update 92), you now have the option to use "ExitOnOutOfMemoryError" option.
http://www.oracle.com/technetwork/java/javase/8u92-relnotes-2949471.html
回答11:
You need to use
-XX:OnOutOfMemoryError=/bin/kill -9 %p
Also if you want to test the changes, you can echo a message before you kill it.
-XX:OnOutOfMemoryError=/bin/date; /bin/echo custom message;/bin/kill -9 %p
回答12:
In the Oracle documentation, OnOutOfMemoryError has the signature:
-XX:OnOutOfMemoryError="<cmd args>; <cmd args>"
Note that here means argument has to be semi-colon separated, instead of space-separated.
So in the mentioned example, it should be:
-XX:OnOutOfMemoryError="kill;-9;%p"