peakRAM
peakRAM copied to clipboard
peakRAM for loops
Thanks for developing this @tpq, I've been using it to benchmark a package I'm developing against a current one. One question though - does peakRAM
work for loops? I'm trying to understand why these two calls give different outputs.
mem <- peakRAM({
for(i in 1:10) {
mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 10008235
mem <- peakRAM({
for(i in 1:5) {
mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 5006635, shouldn't this be the same?
Thanks Dan! I'm glad you've found the package helpful. I made it exactly for that purpose!
This is a very interesting discovery, and I'm afraid that I don't have a very good answer. I am the first to admit that peakRAM
is a little bit hacky. It basically works in 4 steps: (1) Run the garbage collector to reset, (2) Call the function, (3) Run the garbage collector again, (4) Take the difference of Step 3 and Step 1.
It compares the before & after RAM usage recorded by the garbage collector as a proxy for the actual RAM used. I have read that this can be misleading at times. I am unsure whether the for-loop is one example.
Interestingly, when I run it locally I get slightly different results:
mem <- peakRAM({
for(i in 1:2) {
x <- mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 15.3
mem <- peakRAM({
for(i in 1:5) {
x <- mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 38.2
mem <- peakRAM({
for(i in 1:10) {
x <- mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 53.4
mem <- peakRAM({
for(i in 1:20) {
x <- mean(rnorm(1e6))
}
})
mem$Peak_RAM_Used_MiB # 53.4
(FWIW I am using the latest version on GitHub with R 3.6.3)