Frantisek Bartos
2020-Oct-15 22:34 UTC
[R] calling stats::optim from Rcpp causes memory leak
Hi, in part of my code I need to optimize a function from within Rcpp (I followed the 2nd answer here https://stackoverflow.com/questions/48348079/applying-the-optim-function-in-r-in-c-with-rcpp). However, I found that the function leaks memory (very little, but it compounds if it's a part of repeatedly running simulation). I created a minimal reproducible example, but I have no idea where does the leak originate. Could I ask for guidance on dealing with this issue? Rcpp code: #include <Rcpp.h> using namespace Rcpp; double objective_function(double x){ double obj = .666 - x; return pow(obj, 2); } // [[Rcpp::export(leak)]] double leak(double a, double b){ // Extract R's optim function Rcpp::Environment stats("package:stats"); Rcpp::Function optim = stats["optim"]; // Call the optim function from R in C++ Rcpp::List opt_results = optim(Rcpp::_["par"] = .5, Rcpp::_["fn"] Rcpp::InternalFunction(&objective_function), Rcpp::_["method"] = "Brent", Rcpp::_["lower"] = a, Rcpp::_["upper"] = b); // Extract out the estimated parameter values double mu = opt_results[0]; // Return estimated values return mu; } R code: repeat{ leak(0, 1) } When I look at my task manager, the MB usage under Rstudio R Session is steadily increasing (which doesn't happen if I use any other function that just returns a value). Thanks, Frantisek Bartos [[alternative HTML version deleted]]
Seemingly Similar Threads
- [PATCH] drm/nouveau: bail out of nouveau_channel_new if channel init fails
- [PATCH] drm/nouveau: bail out of nouveau_channel_new if channel init fails
- No announcement for kernel 3.10.0-1062.12.1.el7
- [Rcpp-devel] Setting the R random seed from Rcpp
- [PATCH] drm/nouveau: bail out of nouveau_channel_new if channel init fails