xtensor-r icon indicating copy to clipboard operation
xtensor-r copied to clipboard

memory leak when using sorting methods on `rtensor`?

Open ThibHlln opened this issue 1 year ago • 1 comments

Hi,

When I am using xt::sort or xt::quantile on xt::rtensor, there seems to be some form of memory leak.

For example, the following C++ function:

#include <xtensor/xsort.hpp>
#include <xtensor-r/rtensor.hpp>
#include <xtensor-r/rarray.hpp>

#include <Rcpp.h>

// [[Rcpp::plugins(cpp14)]]

// [[Rcpp::export("sort_cpp")]]
xt::rarray<double> sort_cpp(xt::rtensor<double, 4> q)
{
  xt::rarray<double> arr = xt::sort(q, 3);
  return arr;
}

when used repeatedly in R (even with explicit garbage collection), e.g.:

library(myTestLib)

q = array(
  data=rep(c(0.6), 10 * 2 * 11 * 2080),
  dim=c(10, 2, 11, 2080)
)

for (i in 1:1000) 
{
  myTestLib::sort_cpp(q)
  gc()
}

will gradually use more and more memory as one progresses through the loop.

I have put together a small repository to demonstrate the potential memory leak: https://github.com/ThibHlln/myTestLib

Are you able to reproduce my problem? Do you notice a memory leak too?

Thanks.

ThibHlln avatar May 15 '23 16:05 ThibHlln