问题
I want to determine the maximum size of the stack programmatically from within Java (the size set by -Xss). How do I do this?
Alternatively, as my Java module also uses a native code module, I would be able to do this via JNI; but how?
回答1:
Use ManagementFactory.getRuntimeMXBean().getInputArguments()
to access all arguments passed to VM.
回答2:
Maybe not best practice but certainly straight-forward: I'd write a recursive method counting up a value, recurring until java.lang.StackOverflowError and look at the counter.
public class Test
{
static void recur(AtomicInteger start)
{
start.incrementAndGet();
recur(start);
}
public static void main(String[] args) throws ParseException
{
AtomicInteger start=new AtomicInteger(0);
try
{
recur(start);
}
catch (java.lang.StackOverflowError e)
{
/**/
}
System.out.println(start);
}
}
来源:https://stackoverflow.com/questions/7487767/how-to-determine-the-maximum-stack-size-limit