Memory usage in simulation

23 views
Skip to first unread message

Zirou Zhou

unread,
May 27, 2026, 6:35:07 PM (6 days ago) May 27
to nimble-users
Dear developer,

When I run the simulation with `nimbleMCMC`, it crashes during the Monte Carlo process, often due to running out of memory.

Daniel Turek

unread,
May 28, 2026, 7:30:09 AM (5 days ago) May 28
to Zirou Zhou, nimble-users
Zirou, thank you for your question, and your use of nimble.

Are you able to provide more information about your simulation?  It sounds like the simulation is exhausting the available memory, but without more information about your simulation - or best a script of R code which reproduces this problem - it's very difficult to help solve this problem.

Thank you,
Daniel


On Wed, May 27, 2026 at 6:35 PM Zirou Zhou <zrra...@gmail.com> wrote:
Dear developer,

When I run the simulation with `nimbleMCMC`, it crashes during the Monte Carlo process, often due to running out of memory.

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/d429a1d7-860b-4003-9b4e-5a393626569cn%40googlegroups.com.

Zirou Zhou

unread,
May 28, 2026, 10:50:28 AM (5 days ago) May 28
to nimble-users
Hi,

My script for nimbleMCMC is like this, and in each MC loop, I will regenerate the x, y, z, and area information
`code <- nimbleCode({
 
  beta0 ~ dnorm(0, sd = 5)
  beta1 ~ dnorm(0, sd = 5)
 
  gamma0 ~ dnorm(0, sd = 5)
  gamma1 ~ dnorm(0, sd = 5)
 
  phi ~ dgamma(2, 0.05)
  
  alphaDP ~ dgamma(1, 10)
 
  for (h in 1:(K - 1)) {
    v[h] ~ dbeta(1, alphaDP)
  }
 
  v[K] <- 1
 
  stick_left[1] <- 1
 
  for (h in 1:(K - 1)) {
    omega[h] <- v[h] * stick_left[h]
    stick_left[h + 1] <- stick_left[h] * (1 - v[h])
  }
 
  omega[K] <- stick_left[K]
   
  for (h in 1:K) {
   
    sigma_b[h] ~ dunif(0.001, 2)
    sigma_m[h] ~ dunif(0.001, 2)
   
    rho[h] ~ dunif(-0.95, 0.95)
   
    cond_coef[h] <- rho[h] * sigma_m[h] / sigma_b[h]
    cond_sd_m[h] <- sigma_m[h] * sqrt(1 - rho[h] * rho[h])
  }
 
  for (a in 1:A) {
   
    cl[a] ~ dcat(omega[1:K])
   
    b_raw[a] ~ dnorm(0, sd = sigma_b[cl[a]])
   
    mean_m_cond[a] <- cond_coef[cl[a]] * b_raw[a]
   
    m_raw[a] ~ dnorm(mean_m_cond[a], sd = cond_sd_m[cl[a]])
   
    b[a] <- b_raw[a]
    m[a] <- m_raw[a]
  }
 
  for (j in 1:N) {
   
    logit(mu[j]) <- beta0 + beta1 * x[j] + b[area[j]]
   
    logit(p_one[j]) <- gamma0 + gamma1 * z[j] + m[area[j]]
   
    y[j] ~ dOneInflBeta(p_one[j], mu[j], phi)
  }
})

constants <- list(
  N = N,
  A = A,
  K = K,
  area = area_samp,
  x = x_samp,
  z = z_samp
)

data <- list(
  y = y_samp
)

inits_function <- function() {
  list(
    beta0 = 0,
    beta1 = 0,
    gamma0 = 0,
    gamma1 = 0,
    phi = 40,
   
    alphaDP = 0.5,
   
    v = c(rbeta(K - 1, 1, 2), 1),
   
    sigma_b = runif(K, 0.05, 0.5),
    sigma_m = runif(K, 0.05, 0.5),
    rho = runif(K, -0.3, 0.3),
   
    cl = sample(1:K, A, replace = TRUE),
   
    b_raw = rnorm(A, 0, 0.1),
    m_raw = rnorm(A, 0, 0.1)
  )
}

monitors <- c(
  "beta0", "beta1",
  "gamma0", "gamma1",
  "phi",
  "alphaDP",
  "omega",
  "sigma_b", "sigma_m", "rho",
  "b", "m",
  "cl"
)


fit <- nimbleMCMC(
  code = code,
  constants = constants,
  data = data,
  inits = inits_function,
  monitors = monitors,
  niter = 6000,
  nburnin = 2000,
  thin = 2,
  nchains = 3,
  samplesAsCodaMCMC = TRUE)`

Daniel Turek

unread,
May 29, 2026, 7:35:05 AM (4 days ago) May 29
to Zirou Zhou, nimble-users
Zirou, thanks for the additional information.  If you're regenerating x, y, z, and area information repeatedly, and you're encountering memory problems, perhaps you could try using the R "garbage collection" using the gc() command, to free up memory during your simulation?  Another option might be to save the posterior samples to a file (save your "fit" object, which is the object returned from nimbleMCMC), and then remove this object using rm("fit"), and then call gc() ?  This might help free up memory during your simulation.  Those are the ideas which immediately come to mind.

HTH,
Daniel


Reply all
Reply to author
Forward
0 new messages