[R] Is there a way to detect when gc() is called?

Thomas Lumley tlumley at u.washington.edu
Mon Nov 12 19:20:24 CET 2001

On Mon, 12 Nov 2001, Henrik Bengtsson wrote:

> I would like to run a method, which removes obsolete/redundant data in my
> data structures, when gc() will/is/has been called. In other words, is it
> possible to make the garbage collector to trig another function?

There is a finalizer mechanism now in R. I don't know if the documentation
at http://developer.r-project.org is completely up to date, but it should
show you were to look (mostly in memory.c)

Finalizers don't quite do what you want, because it is very difficult.
It turns out to be impractical to guarantee that every finalizer is run
exactly once. Languages have to choose between running it at least once
and at most once.  As I understand it (Luke Tierney is the authority) R
follows Java in ensuring that the finalizer is run at most once, but some
objects that are removable may never be removed.


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list