Long duration then crashes

I run this code on RStudio cloud using 8 CPUs and it takes about 8 hours. However, most of the times it crashes suddenly anytime during the 8 hours.

My questions:

  1. Can the code be modified to take less time?
  2. Why does the cloud crashes?
library(forecast)
library(statnet)

stergm.sim.fun<-function(coef.diss = NULL, coef.form = NULL, density = NULL, start.net=NULL,
                         time.slices = time.slices, seed = NULL, nsim = 1,nw.start=NULL, output = c("networkDynamic"),
                         return.type = c("gden")){
  net.sim<-simulate(start.net,
                    formation = ~edges,  
                    dissolution = ~edges,
                    monitor = "all",
                    coef.form = coef.form,
                    coef.diss = coef.diss,
                    time.slices = time.slices,
                    nsim = nsim,
                    seed = seed,
                    output = output
                     )
stat.density <- as.vector(tSnaStats(net.sim, snafun='gden'))
if(return.type=="gdensity"){return(gdensity = stat.density)}
}

L<- 5
noruns<-1000
ucl<- L*sigma
rl.vec<-matrix(0,nrow=noruns,ncol=1)
for (i in 1:noruns){
resid<-0
rl<-0
forecast.vec<-forecast(model,h=10000)$mean
while (abs(resid)<=ucl) {
  stat<-stergm.sim.fun(coef.diss = coef.diss.3, coef.form = NULL, density = D, 
                       start.net = last.net, time.slices = 1, seed = NULL, nsim = 1)
  forecast.stat<-forecast.vec[rl+1]
  resid<-stat-forecast.stat
  rl<-rl+1
  } 
rl.vec[i,1]<-rl
}
rl.vec

The output is a vector (rl.vec) consisting of 1000 values.

Thanks in advance.