title: R memory management / cannot allocate vector of size n Mb
body: <p>I am running into issues trying to use large objects in R. For example:</p>
  
  <pre><code>&gt; memory.limit(4000)
  &gt; a = matrix(NA, 1500000, 60)
  &gt; a = matrix(NA, 2500000, 60)
  &gt; a = matrix(NA, 3500000, 60)
  Error: cannot allocate vector of size 801.1 Mb
  &gt; a = matrix(NA, 2500000, 60)
  Error: cannot allocate vector of size 572.2 Mb # Can't go smaller anymore
  &gt; rm(list=ls(all=TRUE))
  &gt; a = matrix(NA, 3500000, 60) # Now it works
  &gt; b = matrix(NA, 3500000, 60)
  Error: cannot allocate vector of size 801.1 Mb # But that is all there is room for
  </code></pre>
  
  <p>I understand that this is related to the difficulty of obtaining contiguous blocks of memory (from <a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/Memory-limits.html" rel="noreferrer">here</a>):</p>
  
  <blockquote>
    <p>Error messages beginning cannot
    allocate vector of size indicate a
    failure to obtain memory, either
    because the size exceeded the
    address-space limit for a process or,
    more likely, because the system was
    unable to provide the memory. Note
    that on a 32-bit build there may well
    be enough free memory available, but
    not a large enough contiguous block of
    address space into which to map it.</p>
  </blockquote>
  
  <p>How can I get around this? My main difficulty is that I get to a certain point in my script and R can't allocate 200-300 Mb for an object... I can't really pre-allocate the block because I need the memory for other processing. This happens even when I dilligently remove unneeded objects.</p>
  
  <p>EDIT: Yes, sorry: Windows XP SP3, 4Gb RAM, R 2.12.0:</p>
  
  <pre><code>&gt; sessionInfo()
  R version 2.12.0 (2010-10-15)
  Platform: i386-pc-mingw32/i386 (32-bit)
  
  locale:
  [1] LC_COLLATE=English_Caribbean.1252  LC_CTYPE=English_Caribbean.1252   
  [3] LC_MONETARY=English_Caribbean.1252 LC_NUMERIC=C                      
  [5] LC_TIME=English_Caribbean.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base
  </code></pre>