오류 : C 스택 사용량이 한계에 너무 가깝습니다.
R에서 상당히 깊은 재귀 코드를 실행하려고 시도하고 있는데이 오류가 계속 발생합니다.
오류 : C 스택 사용량이 한계에 너무 가깝습니다.
내 결과 CStack_info()는 다음과 같습니다.
Cstack_info()
size current direction eval_depth
67108864 8120 1 2
내 컴퓨터에 충분한 메모리가 있습니다. R에 대한 CStack을 증가시킬 수있는 방법을 찾으려고합니다.
편집 : 누군가 재현 가능한 예를 요청했습니다. 다음은 문제를 일으키는 몇 가지 기본 샘플 코드입니다. f (1,1)을 몇 번 실행하면 오류가 발생합니다. 이미 --max-ppsize = 500000 및 options (expressions = 500000)를 설정 했으므로이를 설정하지 않으면 대신이 두 가지 중 하나에 대한 오류가 발생할 수 있습니다. 보시다시피 재귀는 여기에서 꽤 깊어 질 수 있으며 일관되게 작동하는 방법을 모릅니다. 감사.
f <- function(root=1,lambda=1) {
x <- c(0,1);
prob <- c(1/(lambda+1),lambda/(lambda+1));
repeat {
if(root == 0) {
break;
}
else {
child <- sample(x,2,replace=TRUE,prob);
if(child[1] == 0 && child[2] == 0) {
break;
}
if(child[1] == 1) {
child[1] <- f(root=child[1],lambda);
}
if(child[2] == 1 && child[1] == 0) {
child[2] <- f(root=child[2],lambda);
}
}
if(child[1] == 0 && child[2] == 0) {
break;
}
if(child[1] == 1 || child[2] == 1) {
root <- sample(x,1,replace=TRUE,prob);
}
}
return(root)
}
스택 크기는 운영 체제 매개 변수이며 프로세스별로 조정 가능합니다 (참조 setrlimit(2)). 내가 말할 수있는 한 R 내에서 조정할 수는 없지만 ulimit명령을 사용하여 R을 시작하기 전에 쉘에서 조정할 수 있습니다 . 다음과 같이 작동합니다.
$ ulimit -s # print default
8192
$ R --slave -e 'Cstack_info()["size"]'
size
8388608
8388608 = 1024 * 8192; R은와 동일한 값을 인쇄 ulimit -s하지만 킬로바이트 대신 바이트 단위 로 인쇄합니다 .
$ ulimit -s 16384 # enlarge stack limit to 16 megs
$ R --slave -e 'Cstack_info()["size"]'
size
16777216
이 설정을 영구적으로 조정하려면 ulimit셸 시작 파일에 명령을 추가하여 로그인 할 때마다 실행되도록합니다. 정확히 어떤 셸을 가지고 있는지에 따라 다르기 때문에 그보다 더 구체적인 지침을 줄 수는 없습니다. 또한 그래픽 환경에 로그인하는 방법도 모릅니다 (터미널 창에서 R을 실행하지 않는 경우 관련이 있음).
스택 제한에 관계없이 너무 깊은 재귀로 끝날 것이라고 생각합니다. 예를 들어, lambda = Inf 인 경우 f (1)은 무기한 즉시 재귀로 이어집니다. 재귀의 깊이는 무작위로 걷는 것처럼 보이며, 어떤 확률은 r이 더 깊어지고 1-r은 현재 재귀를 끝낼 수 있습니다. 스택 제한에 도달 할 때까지 많은 수의 단계를 '더 깊게'만들었습니다. 이것은 r> 1/2, 그리고 대부분의 시간이 계속 반복된다는 것을 의미합니다.
또한 무한 재귀에도 불구하고 분석적 또는 최소한 수치 적 해를 도출하는 것이 거의 가능한 것 같습니다. p를 f (1) == 1 일 확률로 정의하고, 단일 반복 후 '자식'상태에 대한 암시 적 표현식을 작성하고,이를 p와 동일시하고 풀 수 있습니다. p는 이항 분포에서 단일 무승부에서 성공 확률로 사용할 수 있습니다.
이것은 완전히 다른 이유로 나에게 일어났습니다. 두 열을 결합하는 동안 실수로 매우 긴 문자열을 만들었습니다.
output_table_subset = mutate(big_data_frame,
combined_table = paste0(first_part, second_part, col = "_"))
대신에
output_table_subset = mutate(big_data_frame,
combined_table = paste0(first_part, second_part, sep = "_"))
페이스트가 문제를 일으킨다는 것을 결코 예상하지 못했기 때문에 영원히 알아낼 수있었습니다.
이 오류는 메모리 때문이 아니라 재귀 때문 입니다. 함수가 자신을 호출하고 있습니다. 요점을 설명하기 위해 다음은 서로를 호출하는 두 가지 함수의 최소 예입니다.
change_to_factor <- function(x){
x <- change_to_character(x)
as.factor(x)
}
change_to_character <- function(x){
x <- change_to_factor(x)
as.character(x)
}
change_to_character("1")
Error: C stack usage 7971600 is too close to the limit
The functions will continue to call each other recursively and will theoretically never complete. It is only checks within your system that prevent this from occurring indefinitely and consuming all of the compute resources of your machine. You need to alter the functions to ensure that they don't call itself (or each other) recursively.
I encountered the same problem of receiving the "C stack usage is too close to the limit" error (albeit for another application than the one stated by user2045093 above). I tried zwol's proposal but it didn't work out.
To my own surprise, I could solve the problem by installing the newest version of R for OS X (currently: version 3.2.3) as well as the newest version of R Studio for OS X (currently: 0.99.840), since I am working with R Studio.
Hopefully, this may be of some help to you as well.
One issue here can be that you're calling f inside itself
plop <- function(a = 2){
pouet <- sample(a)
plop(pouet)
}
plop()
Erreur : évaluations trop profondément imbriquées : récursion infinie / options(expressions=) ?
Erreur pendant l'emballage (wrapup) : évaluations trop profondément imbriquées : récursion infinie / options(expressions=) ?
For everyone's information, I am suddenly running into this with R 3.6.1 on Windows 7 (64-bit). It was not a problem before, and now stack limits seem to be popping up everywhere, when I try to "save(.)" data or even do a "save.image(.)". It's like the serialization is blowing these stacks away.
I am seriously considering dropping back to 3.6.0. Didn't happen there.
As Martin Morgan wrote... The problem is that you get too deep inside of recursion. If the recursion does not converge at all, you need to break it by your own. I hope this code is going to work, because It is not tested. However at least point should be clear here.
f <- function(root=1,lambda=1,depth=1) {
if(depth > 256){
return(NA)
}
x <- c(0,1);
prob <- c(1/(lambda+1),lambda/(lambda+1));
repeat {
if(root == 0) {
break;
} else {
child <- sample(x,2,replace=TRUE,prob);
if(child[1] == 0 && child[2] == 0) {
break;
}
if(child[1] == 1) {
child[1] <- f(root=child[1],lambda,depth+1);
}
if(child[2] == 1 && child[1] == 0) {
child[2] <- f(root=child[2],lambda,depth+1);
}
}
if(child[1] == NA | child[2] == NA){
return NA;
}
if(child[1] == 0 && child[2] == 0) {
break;
}
if(child[1] == 1 || child[2] == 1) {
root <- sample(x,1,replace=TRUE,prob);
}
}
return(root)
}
Another way to cause the same problem:
library(debug)
mtrace(lapply)
The recursive call isn't as obvious here.
If you're using plot_ly check which columns you are passing. It seems that for POSIXdt/ct columns, you have to use as.character() before passing to plotly or you get this exception!
참고URL : https://stackoverflow.com/questions/14719349/error-c-stack-usage-is-too-close-to-the-limit
'Program Club' 카테고리의 다른 글
| C #에서 속성 선언의 "new"키워드 (0) | 2020.11.08 |
|---|---|
| 특정 기능에 대해 ECMAscript 엄격 모드를 비활성화 할 수 있습니까? (0) | 2020.11.08 |
| C 및 C ++에서 산술 연산 전에 short를 int로 변환해야하는 이유는 무엇입니까? (0) | 2020.11.08 |
| Spring Boot Controller 엔드 포인트에 대한 단위 테스트를 작성하는 방법 (0) | 2020.11.08 |
| PHP에는 Python의 목록 이해 구문과 동등한 기능이 있습니까? (0) | 2020.11.08 |