Skip to content
Browse files

Use RuntimeMXBean rather than custom propertes for mem/stack errs.

  • Loading branch information...
1 parent 8d82e2e commit 5d86783e2db0879a13feb6037af0891eb24785e5 @headius headius committed Feb 17, 2012
Showing with 47 additions and 25 deletions.
  1. +0 −3 bin/jruby.bash
  2. +47 −22 src/org/jruby/Main.java
View
3 bin/jruby.bash
@@ -299,9 +299,6 @@ if [[ $darwin && -z "$JAVA_ENCODING" ]]; then
java_args=("${java_args[@]}" "-Dfile.encoding=UTF-8")
fi
-# Add a property to report memory max
-JAVA_OPTS="$JAVA_OPTS $JAVA_VM -Djruby.memory.max=${JAVA_MEM:4} -Djruby.stack.max=${JAVA_STACK:4}"
-
# Append the rest of the arguments
ruby_args=("${ruby_args[@]}" "$@")
View
69 src/org/jruby/Main.java
@@ -45,6 +45,8 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -278,52 +280,75 @@ private Status handleUnsupportedClassVersion(UnsupportedClassVersionError ucve)
return new Status(1);
}
+ /**
+ * Print a nicer stack size error since Rubyists aren't used to seeing this.
+ */
private Status handleStackOverflow(StackOverflowError soe) {
- // produce a nicer error since Rubyists aren't used to seeing this
- System.gc();
- String stackMax = SafePropertyAccessor.getProperty("jruby.stack.max");
- String message = "";
- if (stackMax != null) {
- message = " of " + stackMax;
+ String memoryMax = getRuntimeFlagValue("-Xss");
+
+ if (memoryMax != null) {
+ config.getError().println("Error: Your application used more stack memory than the safety cap of " + memoryMax + ".");
+ } else {
+ config.getError().println("Error: Your application used more stack memory than the default safety cap.");
}
- config.getError().println("Error: Your application used more stack memory than the safety cap" + message + ".");
config.getError().println("Specify -J-Xss####k to increase it (#### = cap size in KB).");
+
if (config.isVerbose()) {
config.getError().println("Exception trace follows:");
- soe.printStackTrace();
+ soe.printStackTrace(config.getError());
} else {
config.getError().println("Specify -w for full StackOverflowError stack trace");
}
+
return new Status(1);
}
+ /**
+ * Print a nicer memory error since Rubyists aren't used to seeing this.
+ */
private Status handleOutOfMemory(OutOfMemoryError oome) {
- // produce a nicer error since Rubyists aren't used to seeing this
- System.gc();
- String memoryMax = SafePropertyAccessor.getProperty("jruby.memory.max");
- String message = "";
- if (memoryMax != null) {
- message = " of " + memoryMax;
- }
-
+ System.gc(); // try to clean up a bit of space, hopefully, so we can report this error
+
String oomeMessage = oome.getMessage();
- if (oomeMessage.contains("heap space")) {
- config.getError().println("Error: Your application used more memory than the safety cap" + message + ".");
- config.getError().println("Specify -J-Xmx####m to increase it (#### = cap size in MB).");
- } else if (oomeMessage.contains("PermGen")) {
+
+ if (oomeMessage.contains("PermGen")) { // report permgen memory error
config.getError().println("Error: Your application exhausted PermGen area of the heap.");
- config.getError().println("Specify -J-XX:MaxPermSize=###m to increase it (### = PermGen size in MB).");
+ config.getError().println("Specify -J-XX:MaxPermSize=###M to increase it (### = PermGen size in MB).");
+
+ } else { // report heap memory error
+
+ String memoryMax = getRuntimeFlagValue("-Xmx");
+
+ if (memoryMax != null) {
+ config.getError().println("Error: Your application used more memory than the safety cap of " + memoryMax + ".");
+ } else {
+ config.getError().println("Error: Your application used more memory than the default safety cap.");
+ }
+ config.getError().println("Specify -J-Xmx####m to increase it (#### = cap size in MB).");
}
if (config.isVerbose()) {
config.getError().println("Exception trace follows:");
- oome.printStackTrace();
+ oome.printStackTrace(config.getError());
} else {
config.getError().println("Specify -w for full OutOfMemoryError stack trace");
}
+
return new Status(1);
}
+ private String getRuntimeFlagValue(String prefix) {
+ RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
+
+ for (String param : runtime.getInputArguments()) {
+ if (param.startsWith(prefix)) {
+ return param.substring(prefix.length()).toUpperCase();
+ }
+ }
+
+ return null;
+ }
+
private Status handleMainExit(MainExitException mee) {
if (!mee.isAborted()) {
config.getOutput().println(mee.getMessage());

0 comments on commit 5d86783

Please sign in to comment.
Something went wrong with that request. Please try again.