title: R Cannot allocate memory though memory seems to be available
body: <p>After running several models I need to run a <code>system()</code> command on my R script to shutdown my EC2 instance, but when I get to that point I get:</p>
  
  <pre><code>cannot popen 'ls', probable reason 'Cannot allocate memory'
  </code></pre>
  
  <p>Note: for this question I even tried <code>ls</code> which did not work</p>
  
  <p>The flow of my script is the following</p>
  
  <ul>
  <li>Load Model (about 2GB)</li>
  <li>Mine documents and write to a MySQL database</li>
  </ul>
  
  <p>The above steps are repeated around 20 times with different models with an average size of 2GB each</p>
  
  <ul>
  <li>Terminate the instance</li>
  </ul>
  
  <p>At this point is when I need to call <code>system("sudo shutdown -h now")</code> and nothing happens, but when I try <code>system("sudo shutdown -h now",intern=TRUE)</code> I get the allocation error.</p>
  
  <p>I tried <code>rm()</code> for all my objects just before calling the shutdown, but the same error persists.</p>
  
  <p>Here is some data on my system which is a <strong>large EC2 Ubuntu instance</strong></p>
  
  <pre><code>R version 2.15.1 (2012-06-22)
  Platform: x86_64-pc-linux-gnu (64-bit)
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
   [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
   [7] LC_PAPER=C                 LC_NAME=C                 
   [9] LC_ADDRESS=C               LC_TELEPHONE=C            
  [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
  
  attached base packages:
  [1] splines   stats     graphics  grDevices utils     datasets  methods  
  [8] base     
  
  other attached packages:
   [1] RTextTools_1.3.9   tau_0.0-15         glmnet_1.8         Matrix_1.0-6      
   [5] lattice_0.20-10    maxent_1.3.2       Rcpp_0.9.13        caTools_1.13      
   [9] bitops_1.0-4.1     ipred_0.8-13       prodlim_1.3.2      KernSmooth_2.23-8 
  [13] survival_2.36-14   mlbench_2.1-1      MASS_7.3-21        rpart_3.1-54      
  [17] e1071_1.6-1        class_7.3-4        tm_0.5-7.3         nnet_7.3-4        
  [21] tree_1.0-31        randomForest_4.6-6 SparseM_0.96       RMySQL_0.9-3      
  [25] ggplot2_0.9.1      DBI_0.2-5         
  
  loaded via a namespace (and not attached):
   [1] colorspace_1.1-2   dichromat_1.2-4    digest_0.5.2       grid_2.15.1       
   [5] labeling_0.2       memoise_0.1        munsell_0.3        plyr_1.7.1        
   [9] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     scales_0.2.1      
  [13] slam_0.1-25        stringr_0.6.1    
  </code></pre>
  
  <p>gc() returns</p>
  
  <pre><code>          used (Mb) gc trigger   (Mb)  max used   (Mb)
  Ncells 1143171 61.1    5234604  279.6   5268036  281.4
  Vcells 1055057  8.1  465891772 3554.5 767962930 5859.1
  </code></pre>
  
  <p>I noticed that if I run just 1 model instead of the 20 it works fine, so it might be that memory is not getting free after each run although I did <code>rm()</code> the used objects</p>
  
  <p>I also noticed that if I close R and restart it and then call <code>system()</code> it works. If there is a way to restart R within R then maybe I can add that to my <code>script.sh</code> flow.</p>
  
  <p>Which would be the appropriate way of cleaning all of my objects and letting the memory free for each loop so when I need to call the <code>system()</code> commands there is no memory issue?</p>
  
  <p>Any tip in the right direction will be much appreciated!
  Thanks</p>