Closed J-Sward closed 6 years ago
In R, memory is not freed until a garbage collection event happens. This is normally semi-random, but you can force it to happen if you run gc()
.
After running your code, there are two things that I've noticed:
e1
is in the global environment, so you have this, after running all your code:> ls(all=T)
[1] ".Random.seed" "e1" "TEST_CLASS"
In R, putting code in curly braces does not automatically make for local variables.
TEST_CLASS
has a reference to shared
/e1
. That means that even if you do rm(e1)
after running your code, it won't free memory. It needs to keep this reference because so that if you instantiate another object with c <- TEST_CLASS$new()
, it will have a reference to the original shared
object.
I am unclear on how memory management is implemented with R6 objects that contain shared members. I have included a simple case where the class has two fields, each is instantiated with a large vector to illustrate changes in memory used. Both a private and a shared member of unequal sizes are created, although when the object is destroyed, it appears that its shared member persists in memory.
Since the environment variables are shared across all instances of the objects, it makes sense that these are not destroyed when any one of the objects is destroyed. However, when all objects of the class are destroyed I would like to clean up the shared data as well (without wiping everything out with rm(list = ls()). I have tried assigning these shared members NULL in finalize, and removing them altogether (neither of which worked). I can only guess that the environment variables, unmoored from their defining object, need to be accessed/removed directly.
Any help would be greatly appreciated.
rm(list = ls())
library(pryr)