16

R's memory.size() is a Windows only. For other functions (such as windows()) the help page gives pointer to non-windows counterparts.

But for memory.size() I could find no such pointers.

So here is my question: is there a function to do the same as memory.size() but in linux?

zx8754
  • 52,746
  • 12
  • 114
  • 209
user189035
  • 5,589
  • 13
  • 52
  • 112

3 Answers3

15

I think that this should be handled by the operating system. There is no built-in limit that I know of; if necessary, R will use all the memory that it can get.

To obtain information on the total and/or on the available memory in linux, you can try

system('grep MemTotal /proc/meminfo')

or

system('free -m')

or

system('lshw -class memory')

The last command will complain that you should run this as super-user and it will give a warning that the output may not be accurate; but from my experience it will still provide a fairly useful output.


To obtain information on the memory usage of a running R script one could either monitor the currently used resources by starting top in a separate terminal, or use, e.g., the following system call from within the R script:

system(paste0("cat /proc/",Sys.getpid(),"/status | grep VmSize"))

Hope this helps.

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Thanks! But I am under the impression that `memory.size()` gives the memory footprint of the R proc and that the commands you gave give the memory limits assigned to R (something more akin to R/windows` `memory.limit()` command) – user189035 Mar 18 '16 at 10:33
  • @user189035 There seems to be a misunderstanding. There's no memory limit assigned to an R process except for the total available memory. I edited the answer to include a possibility to display the memory used by a running R process. Hope this helps. – RHertel Mar 18 '16 at 17:18
  • The memory.limit() command provides the total size of the machine's memory to an R script. Commands involving system() do not provide anything to an R script, they simply print stuff on the terminal. Completely useless for controlling an R script. – Argent Jul 14 '19 at 23:39
  • 2
    @Argent `memory.limit()` is Windows-specific. The OP asked specifically about Linux. As I write in the answer, the system calls can help to "obtain information", and not to provide anything to an R script. Please read carefully before downvoting. – RHertel Jul 15 '19 at 08:46
9

Using pryr library:

library("pryr")

mem_used()
# 27.9 MB

x <- mem_used()
x
# 27.9 MB
class(x)
# [1] "bytes"

Result is the same as @RHertel's answer, with pryr we can assign the result into a variable.

system('grep MemTotal /proc/meminfo')
# MemTotal:       263844272 kB

To assign to a variable with system call, use intern = TRUE:

x <- system('grep MemTotal /proc/meminfo', intern = TRUE)
x
# [1] "MemTotal:       263844272 kB"
class(x)
# [1] "character"
zx8754
  • 52,746
  • 12
  • 114
  • 209
2

Yes, memory.size() and memory.limit() is not working in linux/unix. I can suggest unix package.

To increase the memory limit in linux:

install.packages("unix") 
library(unix)
rlimit_as(1e12)  #increases to ~12GB

You can also check the memory with this:

rlimit_all()

for detailed information: https://rdrr.io/cran/unix/man/rlimit.html

also you can find further info here: limiting memory usage in R under linux

alika
  • 419
  • 5
  • 7