2009年2月26日星期四

修改堆栈的大小,避免程序调用函数时出现System.StackOverflowException错误

System.StackOverflowException异常通常出现在死循环处。
但是这次我的程序显示异常出现在程序定义行,因为我有一个类里面定义了一个很大的数组。
C#等编译器在编译的时候并没有修改堆栈大小的选项,默认的堆栈大小是1MB,但是可以通过
EditBin.exe程序修改默认的堆栈大小。
如某人的测试:

That works.

Test:

class App {
private static long _Depth = 0;

private static void GoDeep() {
if ((++_Depth % 10000) == 0) System.Console.WriteLine("Depth is " +
_Depth.ToString());
GoDeep();
return;
}

public static void Main() {
try {
GoDeep();
} finally {
}
return;
}
}




editbin /stack:100000,1000 q.exe
Depth is 10000
Depth is 20000

Unhandled Exception: StackOverflowException.

editbin /stack:1000000,1000 q.exe
Depth is 10000
Depth is 20000
Depth is 30000
Depth is 40000
Depth is 50000
Depth is 60000
Depth is 70000
Depth is 80000

Unhandled Exception: StackOverflowException.

没有评论: