Skip to content

Memory Management & Garbage Collection #482

Description

@elbamos

I've been trying to find documentation drilling-down into how Rcpp handles collecting garbage with used c++ objects, and I haven't been able to find anything.

I'm working on an R package (https://github.com/elbamos/largevis) where memory consumption can be very large. I've been writing the computationally-intensive parts in C++ with Rcpp and RcppArmadillo.

To try to reduce memory usage, I've been trying to do more in C++ and less in R and the "R way." In my initial revisions, for example, my principal data structures were NumericMatrix and NumericVector objects. I've gradually been moving from Numeric* objects to arma:: objects. And, where possible, replacing matrices/vectors as data structures with appropriate objects from the C++ standard library. I've also been reducing returns to R, trying to handle more of the steps inside of C++ code without returning variables to R and then passing them back to Rcpp.

Instead of reducing memory usage, this all seems to be having the opposite effect.

Can you clarify when and how objects created in Rcpp get garbage collected?

For example, let's say that inside a C++ loop, in each iteration, I create a NumericMatrix, and arma::mat, and an std:: structure, each of which should only live during a single pass through the loop. Will these be garbage collected while the loop is executing? Does it matter whether the variable is declared inside or outside the loop?

Similarly as to recursion: If I create objects in a recursive function call, are they garbage collected when the creating-call returns, or must they persist until the Rcpp function returns control to R?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions