This help topic is for R version 1.5.0. For the current version of R, try https://stat.ethz.ch/R-manual/R-patched/library/base/html/reg.finalizer.html
reg.finalizer {base}R Documentation

Finalization of objects

Description

Registers an R function to be called upon garbage collection of object.

Usage

  reg.finalizer(e, f)

Arguments

e

Object to finalize. Must be environment or external pointer.

f

Function to call on finalization. Must accept a single argument, which will be the object to finalize.

Value

NULL.

Note

The purpose of this function is mainly to allow objects that refer to external items (a temporary file, say) to perform cleanup actions when they are no longer referenced from within R. This only makes sense for objects that are never copied on assignment, hence the restriction to environments and external pointers.

Examples

 f <- function(e) print("cleaning....")
 g <- function(x){e<-environment(); reg.finalizer(e,f)}
 g() 
 gc() # trigger cleanup

[Package base version 1.5.0 ]