[Rcpp-devel] RcppArmadillo Memory Leak?

Dirk Eddelbuettel edd at debian.org
Mon Nov 18 13:30:10 CET 2013


On 18 November 2013 at 12:41, Alessandro Vitale wrote:
| Hi,
| 
| I was trying RcppArmadillo sparse matrix and found an odd memory behavior.
| 
| I used this code from Romain http://stackoverflow.com/questions/18336021/
| sparse-matrix-conversion-from-matrix-to-armadillo-with-rcpparmadilloextensions-s
|  
| and added the first line to manage large matrices
| 
| #define ARMA_64BIT_WORD
| #include <RcppArmadillo.h>
| // [[Rcpp::depends("RcppArmadillo")]]
| using namespace Rcpp ;
| 
| // [[Rcpp::export]]
| arma::sp_mat sparse( arma::sp_mat A ){
|     A(0,0) = 1;
|     A(1,0) = 2;
|     return A ;
| }
| 
| then tested with this
| 
| library(Matrix)
| testMatrix <- Matrix(data=0,nrow=1e7,ncol=1e7,sparse=TRUE)  # 40MB sparse
| matrix
| gc()
| sparse(testMatrix)
| gc()
| 
| After every call, the memory from gc() is stable, while rsession process memory
| keeps growing of around 100Mb at each call and doesn't decrease after the
| garbage collection.
| 
| Am I doing something wrong?

The sparse() function modifies an object. R uses copy on write, so by
modifying the matrix A, you are creating a new one.  That is normal.  

You then ignore the returned object but not assigning the result from
sparse().  So I think there is no R scope to be finished.  

It might be worth trying if

  a) things are different if you do that from another function and if memory
     gets reclaimed at the end of that function as R then knows to remove objects

  b) things are different with a normal matrix (ie Rcpp::NumericMatrix)

  c) what does valgrind say?  [ In case you can get valgrind or a similar
  tool on OS X, see Section 4.3.2 "Using valgrind" of Writing R Extension
  (which suggested valgrind exists on OS X but was buggy ]

All that said, it is of course entirely possibly that something is overlooked
in our code. 

Dirk

 
| For reference, I'm using Rcpp_0.10.6, RcppArmadillo_0.3.920.1, R 3.0.1 on a Mac
| OSX 10.7.5.
| 
| Thanks for the help!
|  Alessandro
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list