reg.finalizer(e, f, onexit = FALSE)
| e | an environment or an external pointer. |
| f | a single-argument finalization function. The object e will be passed to the function when the object e is no longer in use and has been garbage collected. |
| onexit | a logical value. If TRUE, the finalizer is called at the end of a session if the object is still alive. If FALSE (the default), it clears the finalizer on exit. |
e <- new.env()
reg.finalizer(e, function(x) print("collection is invoked..."))
invisible(gc()) # The environment in e is still alive
e <- NULL # Now the environment is dead
invisible(gc()) # ...and gets collected
# [1] "collection is invoked..."
e <- new.env()
reg.finalizer(e, function(x) print("collection is invoked..."),
onexit = TRUE)
invisible(gc()) # The environment in e is still alive
# q() # When the session ends, the function will run...