2

I have an application that will fail if the -Xss stack size will fail with a StackOverflow error if the value is set below 256k. This is unfortunately a case related to a library com.sun.org.apache.xerces when instantiating a very large XSD validator.

Right now, the pre-defined value for -Xss192k is what we have been using in certain environments, and know that this is causing the problem.

For logging purposes, I was wondering if it was possible to get the max stack size through Java itself, and throw an error on initialization indicating that the provided stack size is insufficient, rather than having the application deploy, then attempt to utilize the stack for this process, and overflow as a result?

The current StackOverflowError in the logs is relatively uninformative for debugging purposes, and I know it is bad programming etiquette to catch an error.

Specifically, what I am trying to do is, in pseudocode:

if(jvm.getMaxStackSize() < "256k") {
    throw new Exception("Insufficient Stack Size for this application. Please set to 256k or more.");
} //stop program from continuing because the stack size will result in a stackoverflow if set too low

Related post Can one obtain actual stack size used by a thread in Java after some time of running? provides info on how to monitor the stack size at run-time, but I don't see a method to get the JVM's pre-set stack size.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Compass
  • 5,867
  • 4
  • 30
  • 42

1 Answers1

3

Try using

RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
List<String> args = runtime.getInputArguments();

This should give you the arguments that were passed to the JVM when it was created.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • It's giving me this: `[-Dfile.encoding=Cp1252, -XX:+ShowCodeDetailsInExceptionMessages]`. So, it's using the default `Xss` i assume. – jumping_monkey May 08 '22 at 02:13