UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

R FAQ
How can I examine and control memory usage in R in Windows?

Note: the code on this page works with R 2.4.1

Viewing and changing memory allocation

To view the current memory limit. The current memory limit is listed in bytes.

memory.limit()
[1] 1610612736

To see the current memory limit in Mb.

round(memory.limit()/1048576.0, 2)
[1] 1536

To increase memory limit (memory limits can be increased, but not decreased). Although it is listed in bytes, new memory size is specified in Mb. Most users will never need to do this. R returns the string "NULL" as the result of running this command, but the memory limit is adjusted.

memory.limit(size=2000)
NULL

To see the usage of the cons cells by SEXPREC type. That is, to see how much memory is taken up, and by what.

memory.profile() 
       NULL      symbol    pairlist     closure environment     promise 
          1        5629      116248        2452        2661        9838 
   language     special     builtin        char     logical     integer 
      38900         209        1218       29797        3405        2582 
     double     complex   character         ...         any        list 
       3995           1       22205           0           0        2376 
 expression    bytecode externalptr     weakref         raw 
          1           0         417         104           1 

Garbage collection ( gb() ) reports on memory usage. It can also prompt R to return memory to the operating system. This is usually done automatically, but can be useful after removing large objects.

gc() 
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 243129  6.5     467875 12.5   350000  9.4
Vcells 108208  0.9     786432  6.0   404125  3.1

Listing and removing objects in memory

To list objects currently in memory.

objects()# returning currently defined objects
[1] "hsb2"     "hsb2.100"

The first line of code removes all objects from memory. After the first line of code, the command objects() returns "character(0)" indicating that there are no objects in memory.

rm(list=ls())
objects()
character(0)

How loading datasets of various sizes impacts memory usage

The following set of output shows memory usage with no objects in memory, with the 200 case hsb2 dataset, and with the first 100 cases of the hsb2 dataset. Note that some types of memory allocation change when a certain type of object is loaded into memory, while others do not. Also note that in some cases, it is the number of variables in the dataset, not the number of rows (cases) that determines how much memory is used.

rm(list=ls())
memory.profile()
       NULL      symbol    pairlist     closure environment     promise
          1        5697      118158        2475        2637        9767
   language     special     builtin        char     logical     integer
      39906         209        1218       30029        3460        2559
     double     complex   character         ...         any        list
       4073           1       22366           0           0        2377
 expression    bytecode externalptr     weakref         raw
          1           0         417         104           1

gc()
         used (Mb) gc trigger (Mb) max used  (Mb)
Ncells 246374  6.6     467875 12.5   350000   9.4
Vcells 109044  0.9    1217086  9.3 64111280 489.2

hsb2<-read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=",", header=T)
memory.profile()
       NULL      symbol    pairlist     closure environment     promise
          1        5697      118172        2475        2637        9767
   language     special     builtin        char     logical     integer
      39906         209        1218       30259        3460        2570
     double     complex   character         ...         any        list
       4073           1       22379           0           0        2378
 expression    bytecode externalptr     weakref         raw
          1           0         417         104           1

gc()
         used (Mb) gc trigger (Mb) max used  (Mb)
Ncells 246643  6.6     467875 12.5   350000   9.4
Vcells 110504  0.9     786432  6.0 64111280 489.2

rm(list=ls())
hsb2.100<-read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=",", header=T, nrows=100)
memory.profile()
       NULL      symbol    pairlist     closure environment     promise
          1        5697      118172        2475        2637        9767
   language     special     builtin        char     logical     integer
      39906         209        1218       30159        3460        2570
     double     complex   character         ...         any        list
       4073           1       22379           0           0        2378
 expression    bytecode externalptr     weakref         raw
          1           0         417         104           1

gc()
         used (Mb) gc trigger (Mb) max used  (Mb)
Ncells 246543  6.6     467875 12.5   350000   9.4
Vcells 109804  0.9     786432  6.0 64111280 489.2

Related links:

Size and storage types of objects

To see the size (in bytes) of a currently defined object. Note, the first line of code defines the object, the second line of code causes R to display the object's size.

hsb2<-read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=",", header=T)
object.size(hsb2)
[1] 18568

To determine how an object is being displayed. Note in the first command, the dataset hsb2 is the object of interest, in the second line of code the variable female which is part of the dataset hsb2 is the object of interest.

storage.mode(hsb2)
[1] "list"

storage.mode(hsb2$female)
[1] "integer"

To change the storage mode of an object. When the object is a variable, this is the same as changing the storage type in other statistical software packages.

storage.mode(hsb2$female)<-"double"
gc(verbose=T)
Garbage collection 19 = 11+0+8 (level 2) ...
222687 cons cells free (47%)
5.2 Mbytes of heap free (85%)
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 245188  6.6     467875 12.5   350000  9.4
Vcells 110816  0.9     786432  6.0   404125  3.1
memory.profile()
       NULL      symbol    pairlist     closure environment     promise
          1        5657      117313        2465        2648        9799
   language     special     builtin        char     logical     integer
      39456         209        1218       30245        3440        2591
     double     complex   character         ...      


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.