Free gc () memory in silence

I am running R code in ubuntu and want to free some memory. After deleting the variables ( rm() ) I call gc() . It seems to work. But how to make it work in silence (i.e. do not report it). I tried setting gcinfo(verbose=FALSE) , but gc() still reports this message.

 gcinfo(verbose=FALSE) # [1] FALSE gc() # used (Mb) gc trigger (Mb) max used (Mb) # Ncells 256641 13.8 467875 25.0 350000 18.7 # Vcells 103826620 792.2 287406824 2192.8 560264647 4274.5 
+5
source share
2 answers

The invisible() function is useful for this. One way is to write a small < gc() cover that returns gc() invisibly without any arguments.

 gcQuiet <- function(quiet = TRUE, ...) { if(quiet) invisible(gc()) else gc(...) } gcQuiet() ## runs gc() invisibly gcQuiet(FALSE) # used (Mb) gc trigger (Mb) max used (Mb) # Ncells 283808 15.2 531268 28.4 407500 21.8 # Vcells 505412 3.9 1031040 7.9 896071 6.9 gcQuiet(FALSE, verbose=TRUE) # Garbage collection 26 = 12+1+13 (level 2) ... # 15.2 Mbytes of cons cells used (53%) # 3.9 Mbytes of vectors used (49%) # used (Mb) gc trigger (Mb) max used (Mb) # Ncells 283813 15.2 531268 28.4 407500 21.8 # Vcells 505412 3.9 1031040 7.9 896071 6.9 
+5
source

The quick and dirty method that I use:

 echo "gc()" > gc.R 

Then you can simply do this:

 source("gc.R", echo=FALSE) 
0
source

Source: https://habr.com/ru/post/1205460/


All Articles