0

I recently downloaded RStudio version 4.2.2 for my experiments since the version I was using was giving me issues. However, I am suprised that the function memory.size() is showing the error, 'memory.size()' is no longer supported. I usually use the function to ascertain the initial and final memory during my machine Learning model fiting so as to know the amount of memory that was used. As suggested in this thread, I have tried it and it couldn't resolve the error. Please since this function is no longer supported, does anyone know which function that I can use? The sample of the code I use for my ML is stated below

> memory.size()
[1] Inf
Warning message:
'memory.size()' is no longer supported

A typical way how I use the function is:

result <- vector("list", 6L)
for (n in 1:5) {
Train <- Training[sample(1:180568, 0.4*nrow(Training)),]
memory.size()
Time1 = Sys.time()
fit_rand <- randomForest(Label~., data= Train)
memory.size()
Time2 = Sys.time()
Train_time = (Time2 - Time1)
print(Train_time)
start_memory = memory.size()
pred_rand <- predict(fit_rand, Testing)
result[[n]] <- confusionMatrix(pred_rand, Testing$Label)
print(result)
Time3 = Sys.time()
Validation_time <- (Time3 - Time2)
print(Validation_time)
stop_memory <- memory.size()
Validation_memory = (stop_memory - start_memory)
}
Frenzy
  • 23
  • 5
  • 1
    see this https://stackoverflow.com/questions/73776480/memory-limit-is-no-longer-supported-work-around – SBMVNO Feb 20 '23 at 00:06
  • I think this is being incorrectly marked as a duplicate (OP wants to know how to record memory usage, not limit/expand available memory ...) – Ben Bolker Feb 27 '23 at 17:44

1 Answers1

0

You could use the peakRAM package (on GitHub here, on CRAN here).

Rather than running memory.size() before and after a particular set of computations, you need to run your computations as an expression within peakRAM, e.g.

library(peakRAM)
peakRAM({
   m <- matrix(rnorm(1e6), nrow = 1e3)
   x <- qr(m)
})
                                Function_Call Elapsed_Time_sec
1 {m<-matrix(rnorm(1e+06),nrow=1000)x<-qr(m)}            0.365
  Total_RAM_Used_MiB Peak_RAM_Used_MiB
1               15.3              30.6

If you look inside peakRAM you'll see that it uses successive calls to gc() in order to determine memory use before/after.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    Could you add some example(s) of how they would use this? Especially if it isn't just a drop-in replacement – camille Feb 20 '23 at 01:49
  • @BenBolker Is that needed? The earlier responses cited R 4.2.0 documentation that suggested the Windows memory allocation was now sufficient. – IRTFM Feb 20 '23 at 02:39
  • OP says they are accustomed to "us[ing] the function to ascertain the initial and final memory during my machine Learning model fiting so as to know the amount of memory that was used"; `peakRAM` should satisfy this use case ... – Ben Bolker Feb 20 '23 at 02:50