3

Assigning the output of a call to the sample() method of a compiled model from the cmdstanr package produces an "environment" in my global environment rather than a model object. Is there some way to see how much memory this environment is using?

For example:

library(cmdstanr)

# Compile an arbitrary model (model needed)
mod <- cmdstan_model(stan_file = "foo.stan")

# Fit model (data needed)
fit <- mod$sample(
  data = bar)

In this case, fit is an environment in my RStudio global environment. Typical objects will show their size in the RStudio environment explorer, but this does not. I'd like to see how much RAM it's occupying.

socialscientist
  • 3,759
  • 5
  • 23
  • 58
  • 2
    Two comments: (1) `cmdstan_model` simply stores the paths to the Stan executable, Stan model and compiled model executable. There should be no significant memory footprint at all. (2) I wouldn't overwrite `mod` when you sample (better to store as e.g. `fit`). The `$sample()` method creates R6 `CmdStanMCMC` objects, which are different from the R6 object returned by `cmdstan_model`. What does `object.size()` on the output of `$sample()` give you? – Maurits Evers Aug 12 '22 at 14:00
  • 1
    you could use `cmdstan` [profiling](https://mc-stan.org/cmdstanr/articles/profiling.html). – Waldi Aug 25 '22 at 07:32
  • 1
    @Waldi That page and the one it links to don't actually have anything on memory profiling -- there's a section about "memory" but it puzzling doesn't actually discuss it as far as I can tell. – socialscientist Aug 28 '22 at 00:50
  • 1
    Agree, chain_stack gives the number of objects allocated, but not their size... – Waldi Aug 28 '22 at 08:20

0 Answers0