How to fix "*** recursive gc invocation" followed by RStudio crash?

I work on a virtual Windows server that I remote into. For the last few weeks many of my RStudio sessions have been crashing within a few minutes to a few hours. The crash almost always happens in the middle of some operation on a relatively large data frame (a few million rows), often a dplyr pipe of several operations or creating a ggplot, where the following is written to the console.

*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation
*** recursive gc invocation

Then, usually within about 10 to 30 seconds, RStudio crashes. When I reopen the project, whatever code edits I hadn't saved are generally gone. Also, even after RStudio crashes, I can see in the Task Manager that R and RStudio processes related to the crashed session are still running, and any RAM the session was using is often still tied up and not available for other processes.

I've also been getting another type of warning I haven't seen before. This one doesn't seem to be associated with crashes, but I'm including an example in case it's relevant. This warning seems to occur in the middle of dplyr pipe operations.:

Warning: stack imbalance in '.Call', 78 then 76
Warning: stack imbalance in '{', 74 then 72
Warning: stack imbalance in '.Call', 76 then 75
Warning: stack imbalance in '{', 72 then 71

I don't have a reproducible example, because this seems to happen randomly and doesn't seem to depend on the specific code I've been running. My local computer is a Macbook Pro and I've never had this happen on it. I'd also never seen the "recursive gc invocation" warning until a few weeks ago. I'm not sure if it coincided with any particular changes in the system or the R setup.

Below are the details for the remote virtual computer. Please let me know if additional information would be helpful. I'd appreciate any ideas you might have on what is causing the problem and how to stop it from happening.

Operating system: Windows Server 2016 Standard, Version 1607, Build 14393.4886
R version 4.1.2
RStudio version: 2021.09.2+382 "Ghost Orchid" Release (fc9e217980ee9320126e33cdf334d4f4e105dc4f, 2022-01-04) for Windows, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36

Session info varies, but below is a typical one. I also load several internal packages that I haven't included below.

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 14393)

Matrix products: default

[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] tools stats graphics grDevices utils datasets methods base

other attached packages:
[1] ggdist_3.0.1 naniar_0.6.1 vroom_1.5.7
[4] patchwork_1.1.1 forcats_0.5.1 stringr_1.4.0
[7] dplyr_1.0.7 purrr_0.3.4 readr_2.1.1
[10] tidyr_1.1.4 tibble_3.1.6 ggplot2_3.3.5
[13] tidyverse_1.3.1 lubridate_1.8.0 readxl_1.3.1
[16] scales_1.1.1 fst_0.9.4 openxlsx_4.2.5
[19] rlang_0.4.12 knitr_1.37
[34] reprex_2.0.1 testthat_3.1.1 devtools_2.4.3
[37] usethis_2.1.5

loaded via a namespace (and not attached):
[1] fs_1.5.2 bit64_4.0.5 httr_1.4.2 rprojroot_2.0.2 backports_1.4.1
[6] utf8_1.2.2 R6_2.5.1 DBI_1.1.2 colorspace_2.0-2 withr_2.4.3
[11] tidyselect_1.1.1 prettyunits_1.1.1 processx_3.5.2 bit_4.0.4 compiler_4.1.2
[16] cli_3.1.0 rvest_1.0.2 xml2_1.3.3 desc_1.4.0 odbc_1.3.3
[21] callr_3.7.0 digest_0.6.29 rmarkdown_2.11 pkgconfig_2.0.3 htmltools_0.5.2
[26] sessioninfo_1.2.2 dbplyr_2.1.1 fastmap_1.1.0 keyring_1.3.0 rstudioapi_0.13
[31] farver_2.1.0 generics_0.1.1 jsonlite_1.7.2 distributional_0.3.0 zip_2.2.0
[36] magrittr_2.0.1 Rcpp_1.0.7 munsell_0.5.0 fansi_0.5.0 visdat_0.5.3
[41] lifecycle_1.0.1 stringi_1.7.6 yaml_2.2.1 pkgbuild_1.3.1 blob_1.2.2
[46] grid_4.1.2 parallel_4.1.2 crayon_1.4.2 haven_2.4.3 hms_1.1.1
[51] ps_1.6.0 pillar_1.6.4 pkgload_1.2.4 glue_1.6.0 evaluate_0.14
[56] remotes_2.4.2 modelr_0.1.8 vctrs_0.3.8 tzdb_0.2.0 cellranger_1.1.0
[61] gtable_0.3.0 assertthat_0.2.1 cachem_1.0.6 xfun_0.29 broom_0.7.11
[66] memoise_2.0.1 ellipsis_0.3.2

1 Like

I had something very similar.
I have started to add gc() to the top of scripts which I know have large objects. That forces a garbage collection before it starts to do the work.
I still have no idea if the issue is in RStudio, my code or the packages. But I don't think this has gone wrong since introducing the gc()
I don't like not knowing the cause - but just needed to get things done

I don't know how to solve your issue but googling turned up
Memory errors when using Rscript with read_tsv_chunked() with trailing tab · Issue #1145 · tidyverse/readr (
This suggests it wouldnt hurt to upgrade cpp11 if you arent on the latest version

I had never seen the error until today. Can confirm that updating my packages, specifically readr, seems to have solved this issue for me.

1 Like

Thanks Nir. It looks like the system is using gcc 8.3.0 (based on Sys.getenv("R_COMPILED_BY")). I'll check with the IT folks about upgrading. My local computer is a Mac and I don't know much about R on Windows. Unfortunately, the IT folks don't either, as I'm the only R user they maintain a virtual machine for. Do you have any tips I can pass along on upgrading the compiler tools? Bear in mind, the virtual machine is isolated from the outside world, so any installations (except for installs directly from the RStudio CRAN mirror) would need to be from local install files (which I can of course download from somewhere, but I can't use any of the automated convenience functions like remotes::install_github(...), etc.).

Thanks Mike. I've also tried using gc(), but I'm having the recursive gc issue even when I do preemptive gc.

Hi Joels, I am having the same problem in my Windows virtual machine recently. For me, the *** recursive gc invocation occurs when using install.packages(). I get some random non-reproducible errors like you as well, with different functions. In Rgui the same code gives no errors, so it seem to me like a Rstudio issue. Have you been able to find a solution?

Unfortunately, I haven't, but I will post it here if I do.

I've filed an issue on this problem at the RStudio github site.

1 Like

I ran into this issue after upgrading to RStudio-2021.09.1-372.
I went back to RStudio-1.3.1093 and it went away.

1 Like


I have the same recursive gc invocation problem.

RStudio IDE crashes shortly after the message is displayed. This happens frequently.

The problem has occurred after the installation of RStudio 2021.09.2+382 "Ghost Orchid" for Windows 10

Using : R-4.1.2 for Windows.

Hope that the RStudio team can resolve this crashing problem quickly.

Based on the issue I filed on Github, it looks like this bug is fixed in RStudio 2022.02.0 Preview.

1 Like

Thank you for filing the issue! I ran RStudio 2022.02.0 Preview and the bug seems fixed now!

1 Like

Thank you
First was the first real issue I had seen with RStudio in 5 years
And only hit 2 other bugs (with R itself and RODBC)

And what I learned is that raising the issue on github works better than raising it here (which I also did).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.