반응형
Java 호출 스택의 최대 깊이는 얼마입니까?
StackOverflowError가 발생하기 전에 호출 스택에 얼마나 깊이 들어가야합니까? 답변 플랫폼이 종속적입니까?
스택에 할당 된 가상 메모리의 양에 따라 다릅니다.
http://www.odi.ch/weblog/posting.php?posting=411
-Xss
VM 매개 변수 또는 Thread(ThreadGroup, Runnable, String, long)
생성자를 사용하여 이를 조정할 수 있습니다 .
내 시스템에서 테스트했지만 상수 값을 찾지 못했습니다. 때로는 8900 호출 후, 때로는 7700, 난수 이후에만 스택 오버플로가 발생합니다.
public class MainClass {
private static long depth=0L;
public static void main(String[] args){
deep();
}
private static void deep(){
System.err.println(++depth);
deep();
}
}
스택 크기는 -Xss
명령 줄 스위치 로 설정할 수 있지만 경험 상으로는 충분히 깊습니다. 수천 건은 아니더라도 수백 건의 호출 깊이입니다. (기본값은 플랫폼에 따라 다르지만 대부분의 플랫폼에서 최소 256k입니다.)
스택 오버플로가 발생하면 99 %의 시간이 코드 오류로 인한 것입니다.
다음 두 호출을 비교합니다.
(1) 정적 메서드 :
public static void main(String[] args) {
int i = 14400;
while(true){
int myResult = testRecursion(i);
System.out.println(myResult);
i++;
}
}
public static int testRecursion(int number) {
if (number == 1) {
return 1;
} else {
int result = 1 + testRecursion(number - 1);
return result;
}
}
//Exception in thread "main" java.lang.StackOverflowError after 62844
(2) 다른 클래스를 사용하는 비 정적 방법 :
public static void main(String[] args) {
int i = 14400;
while(true){
TestRecursion tr = new TestRecursion ();
int myResult = tr.testRecursion(i);
System.out.println(myResult);
i++;
}
}
//Exception in thread "main" java.lang.StackOverflowError after 14002
테스트 재귀 클래스는 public int testRecursion(int number) {
유일한 방법입니다.
참고 URL : https://stackoverflow.com/questions/4734108/what-is-the-maximum-depth-of-the-java-call-stack
반응형
'IT story' 카테고리의 다른 글
Linux에서 공백을 탭으로 바꾸기 (0) | 2020.08.31 |
---|---|
'반응 스크립트 시작'명령은 정확히 무엇입니까? (0) | 2020.08.30 |
POSIX 비동기 I / O (AIO)의 상태는 어떻습니까? (0) | 2020.08.30 |
“raise exception ()”과“raise exception”사이에 괄호없이 차이가 있습니까? (0) | 2020.08.30 |
매크로 정의의 pragma (0) | 2020.08.30 |