After I run a C++ code (through Rcpp code) to create a big text flie in my HD, my computer achieves a high memory usage, but I'm not creating any object in the R session. As shown in the firts code below, I have a high memory use but no big object in my RStudio session. At the same time that I have the results below the task manager shows that the Rstudio R session is using (for example) 6.5GB of memory. I think relevant to share that these memory use decreases slowly after the procedure ends, according to the task manager. It goes to normal short usage after 10-20 minutes.
Does anyone know how colud I stop this memory usage or how to identify why/where/how is this memory being used? I created my code to save the data direct to a txt file in my HD, I want to use less memory as possible.
> memuse::Sys.meminfo()
Totalram: 15.878 GiB
Freeram: 1.547 GiB
>
> for (obj in ls()) {
+ if ( object.size(get(obj))>1000 ){
+ print(c(format(object.size(get(obj)), units = 'Gb'),obj))
+ }
+ }
[1] "0 Gb" "covid_country"
[1] "0 Gb" "gridlogb"
[1] "0 Gb" "gridlogc"
[1] "0 Gb" "loadData"
[1] "0 Gb" "loadDataUS_cases"
[1] "0 Gb" "loadDataUS_deaths"
[1] "0 Gb" "logpost_phi"
[1] "0 Gb" "logpost_txt"
[1] "0 Gb" "postphi_txt"
[1] "0 Gb" "Y"
The C++ code that generates the text file is presented next. It opens another txt file, get the information, transform it an send it to another txt file.
void postphi_txt(NumericVector gridw,
NumericVector gridlogb,
NumericVector gridlogc,
NumericVector gridlogf,
double M, double expmax,
const char* nametxt, const char* nametxt_postphi) {
int nw = gridw.length();
int nb = gridlogb.length();
int nc = gridlogc.length();
int nf = gridlogf.length();
double logpost;
double post;
// postphi file (create)
std::ofstream outfile;
outfile.open(nametxt_postphi);
// logpost file (open)
std::ifstream infile(nametxt);
std::string line;
for (int jw = 0 ; jw < nw ; jw++) {
for (int jb = 0 ; jb < nb ; jb++) {
for (int jc = 0 ; jc < nc ; jc++) {
for (int jf = 0 ; jf < nf ; jf++) {
std::getline(infile, line);
logpost = std::stold(line);
post = exp(logpost - M + expmax);
if (post > 5e-9) {
outfile << std::setprecision(8) << std::fixed << NumericVector::create( post , gridlogb(jb) , gridlogc(jc) , gridlogf(jf) , gridw(jw) ) << "\n";
}
}
}
}
}
outfile.close();
infile.close();
}