如何销毁Ruby对象?
问题描述:
假设有一个简单的对象,例如:
Suppose there is simple object like:
object = Object.new
据我所知,这会在内存 (RAM) 中创建对象.
As I know this creates Object in memory (RAM).
有没有办法从内存中删除这个对象?
Is there a way to delete this object from RAM?
答
除了破解底层 C 代码,没有.垃圾收集由运行时管理,因此您不必担心.这是一个不错的参考 关于 Ruby 2.0 中的算法.
Other than hacking the underlying C code, no. Garbage collection is managed by the runtime so you don't have to worry about it. Here is a decent reference on the algorithm in Ruby 2.0.
一旦您不再引用内存中的对象,垃圾收集器就会开始工作.你应该没事.
Once you have no more references to the object in memory, the garbage collector will go to work. You should be fine.