Unbinding and the garbage collector

  • If you delete the ‘name’ bound to an object, the object still exists
  • R runs a “garbage collector” (GC) to remove these objects when it needs more memory
  • “Looking from the outside, it’s basically impossible to predict when the GC will run. In fact, you shouldn’t even try.”
  • If you want to know when it runs, use gcinfo(TRUE) to get a message printed
  • You can force GC with gc() but you never need to to use more memory within R
  • Only reason to do so is to free memory for other system software, or, to get the message printed about how much memory is being used
gc()
#>           used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells  816298 43.6    1345681 71.9  1345681 71.9
#> Vcells 4649198 35.5   10146329 77.5 10146274 77.5
mem_used()
#> 82.91 MB
  • These numbers will not be what you OS tells you because,
    1. It includes objects created by R, but not R interpreter
    2. R and OS are lazy and don’t reclaim/release memory until it’s needed
    3. R counts memory from objects, but there are gaps due to those that are deleted -> memory fragmentation [less memory actually available they you might think]