0

I'm trying to get a memory profile of my C++ library running under R. Following https://gperftools.github.io/gperftools/heapprofile.html I link against tmalloc, but the build fails.

g++ -std=gnu++11 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro RcppExports.o -L../inst/libs -l:my_package_shared.so -Wl,-rpath,$ORIGIN -ltcmalloc ...

installing to /tmp/bazel/R/lib/my_package
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
src/tcmalloc.cc:332] Attempt to free invalid pointer 0x55a1f8d72e70 
Aborted (core dumped)
ERROR: loading failed
* removing '/tmp/bazel/R/lib/my_package/'

Build system is bazel, build command is

bazel build -c opt --copt="-ggdb" --copt="-fno-omit-frame-pointer" --linkopt="-ltcmalloc" //my_package

Is this doable? Would welcome experiences with heap profiling C++ code called from R. FTR, CPU profiling of Rcpp with gperftools works well. It's memory profiling that's troubling me. Thanks.

msw1520
  • 113
  • 1
  • 7
  • I have used `gpertools` in the past with R and on Linux, see eg slides 21 onwards in https://dirk.eddelbuettel.com/papers/rFinanceHPC.pdf (which is from 2009 (!!)). There is no reason it should no longer work. Note that that works the _call to R to run the code_ and not the linking. That said, the malloc interfaces are generic and you may get `tcmalloc` into but this really is an _R_ question: Rcpp is just a passenger on that journey, using resources provided to it by R. If you change or break those you get to keep the pieces. – Dirk Eddelbuettel Apr 26 '23 at 12:32
  • 1
    Indeed. I am not R expert by any means. But this Rcpp thingy appears to be dynamically loadable module (.so file that is likely dlopened by R). So then linking tcmalloc to that thing is asking for trouble. You'll be dynamically loading malloc (with that rcpp stuff) and that has no chance of working. Consider LDPRELOAD-ing tcmalloc instead for your specific run(s). – Aliaksei Kandratsenka Apr 26 '23 at 23:07

0 Answers0