# Stan memory management

**URL:** <https://discourse.mc-stan.org/t/stan-memory-management/10275>\
**Category:** Developers\
**Created:** [August 16, 2019, 9:31am UTC](https://discourse.mc-stan.org/t/stan-memory-management/10275 "2019-08-16T09:31:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alashworth](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alashworth/32/5459_2.png) [@alashworth](https://discourse.mc-stan.org/u/alashworth)\
**Post date:** [August 16, 2019, 9:31am UTC](https://discourse.mc-stan.org/t/stan-memory-management/10275/1 "2019-08-16T09:31:02Z")

</div>

Hi,

Is there documentation anywhere on `empty_nested` and `recover_memory`? I assume I’m tripping these calls because I’m attempting to put these tests in the same binary, and they aren’t cleaning up correctly after themselves - figured I’d ask here before going over the code.

The following files and fixtures inside trigger an exception because the `SetUp` of the test fixture calls `recover_memory()` while `empty_nested()` is false.

mat/functor/idas\_integrator\_test.cpp  
idas\_ivp\_system\_yy0  
foward\_sensitivity\_theta\_chemical  
forward\_sensitivity\_theta\_prey  
error\_handling

rev/mat/functor/cvodes\_ode\_data\_prim\_test.cpp  
recover\_exception

rev/mat/functor/cvodes\_ode\_data\_rev\_test.cpp  
Basically everything

rev/mat/functor/integrate\_dae\_test.cpp  
idas\_ivp\_system\_yy0  
forward\_sensitivity\_theta  
inconsistent\_ic\_error

rev/mat/fun/append\_row\_test.cpp  
append\_row\_matrix  
append\_row\_row\_vector

rev/mat/fun/append\_col\_test.cpp  
append\_col\_matrix  
append\_col\_row\_vector

rev/mat/fun/matrix\_exp\_multiply\_test.cpp  
matrix\_multiply\_exp\_\_vd\_\_segfault

rev/mat/fun/gp\_periodic\_cov\_test.cpp  
everything

rev/mat/fun/gp\_exp\_quad\_cov\_test.cpp  
everything

rev/mat/fun/cov\_exp\_quad\_test.cpp  
everything

rev/mat/err/check\_consistent\_sizes\_test.cpp  
rev/mat/err/check\_pos\_semidefinite\_test.cpp

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [August 16, 2019, 9:43am UTC](https://discourse.mc-stan.org/t/stan-memory-management/10275/2 "2019-08-16T09:43:16Z")

</div>

I think the tests are sloppy written in that regard. If you put things together that needs clean-up.

You would have to call `recover_memory_nested()` until `empty_nested()` is true. Then you can call `recover_memory()`.

---

<div class="post-metadata">

**Author:** ![alashworth](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alashworth/32/5459_2.png) [@alashworth](https://discourse.mc-stan.org/u/alashworth)\
**Post date:** [August 16, 2019, 9:44am UTC](https://discourse.mc-stan.org/t/stan-memory-management/10275/3 "2019-08-16T09:44:26Z")

</div>

Should I fix it in my monorepo branch or submit an issue to stan math and wait? If I fix it myself, is there any reason I should not change `recover_memory` to recursively call `recover_memory_nested()`?

Are these memory arenas thread safe, i.e. can I run unit tests in parallel, or should I force them to run serially using a mutex or equivalent method?

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [August 16, 2019, 10:30am UTC](https://discourse.mc-stan.org/t/stan-memory-management/10275/4 "2019-08-16T10:30:05Z")

</div>

We should probably fix that in Stan-math… so please file an issue there.

Nothing is thread-safe in Stan-math unless you define `STAN_THREADS` during compilation… but doing that would require proper initialisation per thread; I am not sure we want that in the tests… rather we test things with and without `STAN_THREADS` (at least at the moment).
