gpt4 book ai didi

ruby - 在 Ruby 中删除类实例?

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:34 25 4
gpt4 key购买 nike

我已经阅读了关于断开实例变量的任何引用后在不确定的时间发生 GC 的说明,但是 delete 方法中的第二行是否愚蠢、不必要或彻底?

class MyClass
# new instances added to @@instances
...
def delete
@@instances.delete(self)
self.instance_variables.each{|v| self.instance_variable_set(v,nil)}
end

end

最佳答案

没必要。如果您真的想触发 GC,请使用 GC.startObjectSpace.garbage_collect

并且因为它不能经常被推荐,再次:

http://viewsourcecode.org/why/hacking/theFullyUpturnedBin.html

关于ruby - 在 Ruby 中删除类实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com