Categories
memory-leaks memory-management r

Increasing (or decreasing) the memory available to R processes

156

I would like to increase (or decrease) the amount of memory available to R. What are the methods for achieving this?

    77

    From:

    http://gking.harvard.edu/zelig/docs/How_do_I2.html (mirror)

    Windows users may get the error that R
    has run out of memory.

    If you have R already installed and
    subsequently install more RAM, you may
    have to reinstall R in order to take
    advantage of the additional capacity.

    You may also set the amount of
    available memory manually. Close R,
    then right-click on your R program
    icon (the icon on your desktop or in
    your programs directory). Select
    “Properties”, and then select the
    “Shortcut” tab. Look for the
    “Target” field and after the closing
    quotes around the location of the R
    executible, add

    –max-mem-size=500M

    as shown in the figure below. You may
    increase this value up to 2GB or the
    maximum amount of physical RAM you
    have installed.

    If you get the error that R cannot
    allocate a vector of length x, close
    out of R and add the following line to
    the “Target” field:

    –max-vsize=500M

    or as appropriate. You can always
    check to see how much memory R has
    available by typing at the R prompt

    memory.limit()
    

    which gives you the amount of available memory in MB. In previous versions of R you needed to use: round(memory.limit()/2^20, 2).

    8

    • 8

      Note that on 32-bit Windows, R can only use up to 3Gb of RAM, regardless of how much you have installed. There is a 64-bit version of R for Windows available from REvolution Computing, which runs on 64-bit Windows and can use all the RAM available: revolution-computing.com/products/revolution-enterprise.php

      Sep 8, 2009 at 17:40

    • 6

      Just two notes: (i) memory.limit() only works on Windows, and (ii) the command that will return the proper answer is round(memory.limit(),2) — at least on my version of R (2.8.1).

      Sep 8, 2009 at 18:01

    • 36

      Any suggestion on how to get similar information as memory.limit() working on linux?

      – exl

      Oct 13, 2012 at 2:10

    • 5

      The webpage doesn’t exist anymore. It’s hard for me to follow this without the images. I don’t see a “Shortcut” tab for example. Also, can you use this to decrease the max memory?

      – blakeoft

      Mar 23, 2015 at 20:31


    • 6

      So, what about mac users? Can we do something about it?

      Nov 30, 2015 at 22:49

    61

    Use memory.limit(). You can increase the default using this command, memory.limit(size=2500), where the size is in MB. You need to be using 64-bit in order to take real advantage of this.

    One other suggestion is to use memory efficient objects wherever possible: for instance, use a matrix instead of a data.frame.

    2

    • 3

      I don’t really understand how this works. I ran into this memory limit issue and received the error “Reached total allocation of 8182Mb”. I’m using 64-bit with 8GB ram so I seemed to be SOL. However, for shiggles, I set memory.limit(size=50000)…AND IT WORKED! But why???

      Dec 16, 2015 at 17:52

    • 2

      I know this is 7 years old, but I’m trying to reduce the amount of RAM that can be used by R but whenever I try to reduce the value I get this message (I have 12gb Ram on a 64-bit Windows machine). Warning message: In memory.limit(4095) : cannot decrease memory limit: ignored

      – hellter

      Jun 12, 2016 at 18:32

    15

    For 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