Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peakRAM for loops #3

Open
danxfreeman opened this issue Sep 14, 2020 · 1 comment
Open

peakRAM for loops #3

danxfreeman opened this issue Sep 14, 2020 · 1 comment

Comments

@danxfreeman
Copy link

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?
@tpq
Copy link
Owner

tpq commented Sep 17, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants