假设我定义了一个'foo'带有两个插槽'a'和的 S4 类'b',并定义了一个x类的对象'foo',
setClass(Class = 'foo', slots = c(
a = 'numeric',
b = 'character'
))
x <- new('foo', a = rnorm(1e3L), b = rep('A', times = 1e3L))
format(object.size(x), units = 'auto') # "16.5 Kb"
Run Code Online (Sandbox Code Playgroud)
然后我想'a'从定义中删除插槽'foo'
setClass(Class = 'foo', slots = c(
b = 'character'
))
slotNames(x) # slot 'a' automatically removed!! wow!!!
Run Code Online (Sandbox Code Playgroud)
我看到 R 会自动处理我的对象x并'a'移除插槽。好的!但是等等,对象的大小x并没有减少。
format(object.size(x), units = 'auto') # …Run Code Online (Sandbox Code Playgroud)