[Rcpp-devel] freeing memory

Dirk Eddelbuettel edd at debian.org
Thu Mar 24 20:55:03 CET 2016


Hi there,

On 24 March 2016 at 21:37, rosterma at abo.fi wrote:
| I have used the example rinside_sample1.cpp as a basis for a short 
| routine to transmit data from C++ to R and to recover the results from R 
| to C++. On the face of it, everything works nicely both in our mainframe 
| (linux) computer at ÅAU and on the supercomputers at CSC IT Center of 
| Science (Helsinki). Before proceeding - not being an R expert myself - I 
| wanted to check the memory absorbance of the system. When checking the 
| program flow with Valgrind, the debugger signals that several blocks of 
| data are still reachable when exiting. This cannot be,
| since multiple calls to the routine would quickly absorb the CPU.

valgrind is a very powerful tool. But when you embed R, which is an
interpreted language with its own memory pool and management, you have to
expect a few differences.

Often, when I encounter such an example I try to first form a
baseline. Something like creating a C++ executable instantiating RInside, and
then 'doing nothing'.  Maybe Sys.sleep(5) or something like that.  Run it
under valgrid, not how is want /may be/ unreachable or not freed /when you
have not actually allocated anything/.

Then do the same, but create a matrix or vector with, say, 10e3, then 10e4,
then 10e5, ... elements and look at the output.

| So, how do I free memory for example when allocating it through commands
| such as:
|    Rcpp::NumericMatrix C_R=createMatrix(CATSM_INFO[0],cm,cn);
|    Rcpp::NumericVector v = R.parseEval(str);
|    string result(argv[1]);

The nice thing about C++ is that it has constructors and destructors so /a
priori/ you should not need to do anything.
 
| How should the example rinside_sample1.cpp be modified to get this 
| straight and to avoid the risk of excessive memory absorbance in 
| multiple calls?

Not to sound flippant, but we first need to really demonstrate that there
actually is a leak.

Hope this helps,  Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list