C Stack usage limit

R
Published

January 29, 2026

Abstract

How to resolve the C stack usage is too close to the limit error

Description

The C stack usage is too close to the limit error occurs when running R code that is deeply recursive. Test the R code outside of Posit Workbench to confirm this is an issue in R, rather than in Workbench.

Deep recursion will consume the memory keeping track of the thread’s call stack. You can view the stack size limit from the Linux shell with ulimit -s , or from within R with system("ulimit -s"). Currently most OSes default to 8192 kb (8 megabytes).

Within R, running Cstack_info() will output the stack size and current usage, although the output is in bytes instead of kilobytes.

Recursive functions themselves aren’t the issue, only the depth of the function - which could be infinite. A common suggested solution is to increase the stack limit, although this doesn’t address the recursion problem. 

Solution

Increase the stack size by editing /etc/security/limits.conf with a priviledged user. For more information, see the man pages here: https://linux.die.net/man/5/limits.conf

For example, to raise the stack size from 8MB to 65MB, add or adjust these two lines:

/etc/security/limits.conf
* soft stack 65535
* hard stack 65535

A new login will be required to see the change.

If your R code has infinite or very deep recursion, this configuration changee won’t resolve the issue. We suggest revising your R code to address the issue directly.

See this Stack Overflow post for more information.

If you’re still having issues, you can reach out to Support by opening a ticket.