I want to make a function that imports data in different numbers of batches depending on how much RAM is available on someones system. But how can I find the amount of available RAM in R? I can use memory.size()
but that only works for Windows.
Asked
Active
Viewed 3.0k times
46
![](../../users/profiles/567015.webp)
Sacha Epskamp
- 42,423
- 17
- 105
- 128
-
3You are not going to implement one method to do this. Your going to have to detect the platform then use different methods. – Security Hound Jun 23 '11 at 16:16
-
*"If you have to ask for the price, you cannot afford it."* This looks the wrong approach as memory and resource management is handled by the operating system. As Ramhound said, your approach will become platform-dependent. – Dirk Eddelbuettel Jun 23 '11 at 16:24
-
3The idea is to get a crude value that works somewhat as a default. For example, 1 batch with 12gb, 2 with 6gb, etcetera. Platform can be found with `Sys.info()` so platform dependent approach should be possible right? – Sacha Epskamp Jun 23 '11 at 16:30
3 Answers
45
Given the warnings concerning platform-dependency discussed in the earlier comment, you could for example parse /proc/meminfo
on Linux:
$ grep MemFree /proc/meminfo
MemFree: 573660 kB
$ awk '/MemFree/ {print $2}' /proc/meminfo
565464
You could try the second approach via system(..., intern=TRUE)
, or even via a pipe connection.
Edit some 5+ years later: In R, and just following what the previous paragraph hinted at:
R> memfree <- as.numeric(system("awk '/MemFree/ {print $2}' /proc/meminfo",
+ intern=TRUE))
R> memfree
[1] 3342480
R>
![](../../users/profiles/143305.webp)
Dirk Eddelbuettel
- 331,520
- 51
- 596
- 675
-
5I recommend to use `gc()` before calling this function to get the true amount of available memory. – F. Privé May 01 '17 at 09:00
-
3
-
17for those that prefer copy-pasteable code: `as.numeric(system("awk '/MemFree/ {print $2}' /proc/meminfo", intern=TRUE))` – MichaelChirico Dec 11 '17 at 02:18
-
-
@xiaodai do you know why? I have heard cran doesn't like `system()` calls, but I don't believe there's any complete ban. I think cran libraries need to work across all of windows/osx/unix/linux, perhaps this command stumbles on some platform (I don't know that for sure, just trying to guess why it would be denied). – stevec Apr 17 '20 at 07:28
-
I don't know why but my package got taken down cos I used it. So don't use it in a package. – xiaodai Apr 17 '20 at 09:52
-
@F.Privé How does `gc()` be useful in this context? Should the info be gathered from `Ncells` or `Vcells` output? – Prradep Oct 29 '20 at 10:26
-
1@Prradep It is useful because it gets rid of all the things that can be cleaned up to free some memory. – F. Privé Oct 29 '20 at 14:57
-
Yes, I understand that. How to infer about the RAM availability from this output? – Prradep Oct 30 '20 at 12:07
9
Now you can do that with benchmarkme::get_ram
function.
https://cran.r-project.org/web/packages/benchmarkme/benchmarkme.pdf
![](../../users/profiles/5029763.webp)
user5029763
- 1,753
- 1
- 12
- 19