如何增加Java堆栈的大小?
StackOverflowError.
public class TT {
public static long fact(int n) {
return n < 2 ? 1 : n * fact(n - 1);
}
public static void main(String[] args) {
System.out.println(fact(1 << 15));
}}java -Xss...TT
$ javac TT.java $ java -Xss4m TT
-X...
java version "1.6.0_18"OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1~8.04.3)OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)
java -Xss...
n
fact(1 << 15)
fact(1 << 17)
fact(1 << 18)
fact(1 << 19)
fact(1 << 20)
fact(1 << 21)
fact(1 << 22)
fact(1 << 23)
fact(1 << 24)
fact(1 << 25)
-Xss...StackOverflowError-Xss18m-Xss19m-Xss20m
StackOverflowError
Stackfact
public class TTIterative {
public static long fact(int n) {
if (n < 2) return 1;
if (n > 65) return 0; // Enough powers of 2 in the product to make it (long)0.
long f = 2;
for (int i = 3; i <= n; ++i) {
f *= i;
}
return f;
}
public static void main(String[] args) {
System.out.println(fact(1 << 15));
}}factlongfactBigIntegerlong
www说
qq_花开花谢_0
开心每一天1111
相关分类