# Brms memory (RAM) overload

**URL:** <https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948>\
**Category:** brms\
**Tags:** brms\
**Created:** [July 2, 2023, 3:33am UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948 "2023-07-02T03:33:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Campanelli](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@Luca\_Campanelli](https://discourse.mc-stan.org/u/Luca_Campanelli)\
**Post date:** [July 2, 2023, 3:33am UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/1 "2023-07-02T03:33:03Z")

</div>

Hello All,

It’s been a while since I last used brms, so I’m not sure whether this is a new or an old issue. However, I’ve noticed that brms is using all the available RAM. After a few hundred iterations, it becomes fully occupied. I tried running the program from my laptop, which has 32 GB of RAM, and it was filled after approximately 300 iterations. I also tried from my office computer, which has 254 GB of RAM. It filled up as well after a while, forcing me to stop the sampling. This issue occurred with different models, including the simple example below.

Here are the specs of my laptop:  
Platform: x86\_64-pc-linux-gnu (64-bit)  
Ubuntu 22.04.2 LTS  
RSstudio 2023.06.0, Build 421  
R version 4.3.1  
rstan 2.21.8  
brms 2.19.0

Thank you in advance for any input or advice you may have.  
Luca

```plaintext
# brms: An R Package for Bayesian Multilevel Models using Stan
# https://cran.r-project.org/web/packages/brms/vignettes/brms_overview.pdf
# 4.1. A worked example
library("brms")
data("kidney")
head(kidney, n = 3)

# 4.2. Fitting models with brms
fit1 <- brm(formula = time | cens(censored) ~ age * sex + disease + (1 + age|patient),
            data = kidney, family = lognormal(),
            prior = c(set_prior("normal(0,5)", class = "b"),
                      set_prior("cauchy(0,2)", class = "sd"),
                      set_prior("lkj(2)", class = "cor")),
            warmup = 1000, iter = 2000, chains = 4,
            control = list(adapt_delta = 0.95))

```

---

<div class="post-metadata">

**Author:** ![Luca\_Campanelli](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@Luca\_Campanelli](https://discourse.mc-stan.org/u/Luca_Campanelli)\
**Post date:** [July 3, 2023, 8:14pm UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/2 "2023-07-03T20:14:00Z")

</div>

It looks like when using `backend='cmdstanr'`, it works fine with no memory issues. It must be a bug specific to the default backend.

---

<div class="post-metadata">

**Author:** ![jverissimo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jverissimo/32/3260_2.png) [@jverissimo](https://discourse.mc-stan.org/u/jverissimo)\
**Post date:** [July 12, 2023, 8:16am UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/3 "2023-07-12T08:16:57Z")

</div>

Same issue here. Models that used to run in the same computer a few months ago now fill up the RAM and swap completely, then crash the session (with rstan).

Downgrading brms to 2.17 or 2.18 (from 2.19) does not solve the problem, so I think it may lie elsewhere.

And as reported above, using cmdstanr as a backend solves it.

---

<div class="post-metadata">

**Author:** ![bmfazio](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bmfazio/32/16653_2.png) [@bmfazio](https://discourse.mc-stan.org/u/bmfazio)\
**Post date:** [July 12, 2023, 8:21am UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/4 "2023-07-12T08:21:31Z")

</div>

FYI, I had this same issue but it seems to be fixed with the latest `rstan` development version (2.26). Installation instructions in link below.

> **[RStan Getting Started](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started)**
>
> RStan, the R interface to Stan. Contribute to stan-dev/rstan development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![kayhales](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kayhales](https://discourse.mc-stan.org/u/kayhales)\
**Post date:** [August 16, 2023, 5:43pm UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/5 "2023-08-16T17:43:53Z")

</div>

I still have this issue with the 2.26.1 version of rstan. Might be due to the very limited memory of my laptop (8gb RAM). Any advice on running these models with this little memory, or is my best option just to switch to a better machine?

---

<div class="post-metadata">

**Author:** ![bmfazio](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bmfazio/32/16653_2.png) [@bmfazio](https://discourse.mc-stan.org/u/bmfazio)\
**Post date:** [August 17, 2023, 6:13am UTC](https://discourse.mc-stan.org/t/brms-memory-ram-overload/31948/6 "2023-08-17T06:13:54Z")

</div>

Two things you may quickly want to try before switching machines is to use the `backend = "cmdstanr"` argument or, if you are using a complex model/large datasets/high amount of iterations, first try something simple like estimating a mean (x ~ 1) from a small number of observations.

I haven’t really paid attention to the memory footprint of Stan programs but I would be **very** surprised if a simple model causes trouble with your 8GB.
