[Rd] How to locate references to an environment?

Peter Meilstrup peter@me||@trup @end|ng |rom gm@||@com
Tue May 23 20:29:19 CEST 2023


R developers,

I am trying to track down a memory leak in my R package.

I have a complex object O which comprises a lot of closures and such.
Among which, the object uses an environment E to perform computations
and keep intermediate values in. When O closes/finishes with its task
it nulls out its reference to E so that that intermediate data can be
garbage collected; I've verified that it does null the reference.

However, it seems there is another reference to E floating around. I
can tell because I can ask O to put a large array in E, then tell O to
close, which nulls the reference to E, but then if I serialize(O,
ascii=TRUE) I can still see the array in the output.

Dangling references to E could come from a closure created in E, or an
unforced promise from a function call evaluated in E that created a
closure I still have a reference to, or, ... my question is how do I
locate the reference?

Is there a way to scan the workspace for objects that refer to a given object?

Or is there a tool that will unpack/explain serialize()'s .rds format
in a more human-readable way so that I can tell where the reference to
E occurs?

Peter



More information about the R-devel mailing list