finalize.Object.Rd
Finalizer methods are called just the moment before the object is about to be deleted by the garbage collector.
If creating a custom finalize()
method for a subclass
in a package, then it needs to be exported in the NAMESPACE of
that package. If not, it will not be found nor called and
there will be no error message.
A finalizer method should never be called explicitly!
# S3 method for class 'Object'
finalize(this, ...)
For more information see Object
.
setConstructorS3("MyClass", function() {
extend(Object(), "MyClass")
})
setMethodS3("finalize", "MyClass", function(this, ...) {
cat(as.character(this), "is about to be removed from the memory!\n")
})
#> NULL
o <- MyClass()
o <- MyClass()
o <- MyClass()
o <- MyClass()
gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 1000385 53.5 1957258 104.6 1957258 104.6
#> Vcells 1822363 14.0 8388608 64.0 3140456 24.0
## MyClass: 0x01BE602C is about to be removed from the memory!
## MyClass: 0x01BFF634 is about to be removed from the memory!
## MyClass: 0x01C13584 is about to be removed from the memory!
## used (Mb) gc trigger (Mb)
## Ncells 229903 6.2 467875 12.5
## Vcells 53725 0.5 786432 6.0
rm(o)
## MyClass: 0x01C578B0 is about to be removed from the memory!
## used (Mb) gc trigger (Mb)
## Ncells 229903 6.1 467875 12.3
## Vcells 53725 0.5 786432 6.0