C Stack usage limit



The "C stack usage is too close to the limit" error occurs when running R code that is deeply recursive. This appears to be an R programming question; it's unlikely that the Posit product itself has any impact here.


To explain further, a deep recursion can consume the memory region that keeps track of the thread's call stack. You can view the Stack size limit from the Linux shell withulimit -s , or from within R system("ulimit -s") . This is usually 8192 kb (8 megabytes) on a Linux OS.


Cstack_info() Executed in R will output the stack size and current usage. (output is in bytes instead of kilobytes)


The recursive function isn't the issue, only the depth of it (which may be infinite). The most common fix you'll find suggested is to raise the stack limit though this isn't addressing the problem in most cases.


You can increase the stack size if you don't believe this is an infinite recursion. This would be general Linux administration and can be done using the /etc/security/limits.conf​ configuration file. For more information, see the man pages here: https://linux.die.net/man/5/limits.conf

e.g., to raise the stack size from 8MB to 65MB



* soft stack 65535
* hard stack 65535
# End of file

(then login again)


If the deep recursion is infinite, then the above won't resolve the issue, regardless of the stack size. Infinite recursion can occur when the recursion does not reduce the problem in a manner that allows it to converge eventually. We suggest adjusting the R code to address the issue directly to avoid going too deep in the recursion. While the specific steps to take within your code are outside of our support, we hope the above is helpful to understand and resolve the issue internally.


Some helpful references are below: