Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 5d86783e2db0879a13feb6037af0891eb24785e5 1 parent 8d82e2e
Charles Oliver Nutter headius authored

Showing 2 changed files with 47 additions and 25 deletions. Show diff stats Hide diff stats

  1. +0 3  bin/jruby.bash
  2. +47 22 src/org/jruby/Main.java
3  bin/jruby.bash
@@ -299,9 +299,6 @@ if [[ $darwin && -z "$JAVA_ENCODING" ]]; then
299 299 java_args=("${java_args[@]}" "-Dfile.encoding=UTF-8")
300 300 fi
301 301
302   -# Add a property to report memory max
303   -JAVA_OPTS="$JAVA_OPTS $JAVA_VM -Djruby.memory.max=${JAVA_MEM:4} -Djruby.stack.max=${JAVA_STACK:4}"
304   -
305 302 # Append the rest of the arguments
306 303 ruby_args=("${ruby_args[@]}" "$@")
307 304
69 src/org/jruby/Main.java
@@ -45,6 +45,8 @@
45 45 import java.io.FileInputStream;
46 46 import java.io.FileNotFoundException;
47 47 import java.io.IOException;
  48 +import java.lang.management.ManagementFactory;
  49 +import java.lang.management.RuntimeMXBean;
48 50 import java.util.ArrayList;
49 51 import java.util.List;
50 52 import java.util.Map;
@@ -278,52 +280,75 @@ private Status handleUnsupportedClassVersion(UnsupportedClassVersionError ucve)
278 280 return new Status(1);
279 281 }
280 282
  283 + /**
  284 + * Print a nicer stack size error since Rubyists aren't used to seeing this.
  285 + */
281 286 private Status handleStackOverflow(StackOverflowError soe) {
282   - // produce a nicer error since Rubyists aren't used to seeing this
283   - System.gc();
284   - String stackMax = SafePropertyAccessor.getProperty("jruby.stack.max");
285   - String message = "";
286   - if (stackMax != null) {
287   - message = " of " + stackMax;
  287 + String memoryMax = getRuntimeFlagValue("-Xss");
  288 +
  289 + if (memoryMax != null) {
  290 + config.getError().println("Error: Your application used more stack memory than the safety cap of " + memoryMax + ".");
  291 + } else {
  292 + config.getError().println("Error: Your application used more stack memory than the default safety cap.");
288 293 }
289   - config.getError().println("Error: Your application used more stack memory than the safety cap" + message + ".");
290 294 config.getError().println("Specify -J-Xss####k to increase it (#### = cap size in KB).");
  295 +
291 296 if (config.isVerbose()) {
292 297 config.getError().println("Exception trace follows:");
293   - soe.printStackTrace();
  298 + soe.printStackTrace(config.getError());
294 299 } else {
295 300 config.getError().println("Specify -w for full StackOverflowError stack trace");
296 301 }
  302 +
297 303 return new Status(1);
298 304 }
299 305
  306 + /**
  307 + * Print a nicer memory error since Rubyists aren't used to seeing this.
  308 + */
300 309 private Status handleOutOfMemory(OutOfMemoryError oome) {
301   - // produce a nicer error since Rubyists aren't used to seeing this
302   - System.gc();
303   - String memoryMax = SafePropertyAccessor.getProperty("jruby.memory.max");
304   - String message = "";
305   - if (memoryMax != null) {
306   - message = " of " + memoryMax;
307   - }
308   -
  310 + System.gc(); // try to clean up a bit of space, hopefully, so we can report this error
  311 +
309 312 String oomeMessage = oome.getMessage();
310   - if (oomeMessage.contains("heap space")) {
311   - config.getError().println("Error: Your application used more memory than the safety cap" + message + ".");
312   - config.getError().println("Specify -J-Xmx####m to increase it (#### = cap size in MB).");
313   - } else if (oomeMessage.contains("PermGen")) {
  313 +
  314 + if (oomeMessage.contains("PermGen")) { // report permgen memory error
314 315 config.getError().println("Error: Your application exhausted PermGen area of the heap.");
315   - config.getError().println("Specify -J-XX:MaxPermSize=###m to increase it (### = PermGen size in MB).");
  316 + config.getError().println("Specify -J-XX:MaxPermSize=###M to increase it (### = PermGen size in MB).");
  317 +
  318 + } else { // report heap memory error
  319 +
  320 + String memoryMax = getRuntimeFlagValue("-Xmx");
  321 +
  322 + if (memoryMax != null) {
  323 + config.getError().println("Error: Your application used more memory than the safety cap of " + memoryMax + ".");
  324 + } else {
  325 + config.getError().println("Error: Your application used more memory than the default safety cap.");
  326 + }
  327 + config.getError().println("Specify -J-Xmx####m to increase it (#### = cap size in MB).");
316 328 }
317 329
318 330 if (config.isVerbose()) {
319 331 config.getError().println("Exception trace follows:");
320   - oome.printStackTrace();
  332 + oome.printStackTrace(config.getError());
321 333 } else {
322 334 config.getError().println("Specify -w for full OutOfMemoryError stack trace");
323 335 }
  336 +
324 337 return new Status(1);
325 338 }
326 339
  340 + private String getRuntimeFlagValue(String prefix) {
  341 + RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
  342 +
  343 + for (String param : runtime.getInputArguments()) {
  344 + if (param.startsWith(prefix)) {
  345 + return param.substring(prefix.length()).toUpperCase();
  346 + }
  347 + }
  348 +
  349 + return null;
  350 + }
  351 +
327 352 private Status handleMainExit(MainExitException mee) {
328 353 if (!mee.isAborted()) {
329 354 config.getOutput().println(mee.getMessage());

0 comments on commit 5d86783

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