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 815149 43.6 1321315 70.6 1321315 70.6
#> Vcells 4651632 35.5 10146329 77.5 10146090 77.5
mem_used()
#> 82.87 MB
- These numbers will not be what you OS tells you because,
- It includes objects created by R, but not R interpreter
- R and OS are lazy and don’t reclaim/release memory until it’s needed
- 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]