The main purpose of this function is 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.
Inter alia, it provides a way to program code to be run at
the end of an R session without manipulating .Last
.
For use in a package, it is often a good idea to set a finalizer on an
object in the namespace: then it will be called at the end of the
session, or soon after the namespace is unloaded if that is done
during the session.