ruby 如何销毁Ruby对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19523039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy Ruby object?
提问by Иван Бишевац
Suppose there is simple object like:
假设有一个简单的对象,如:
object = Object.new
As I know this creates Object in memory (RAM).
据我所知,这会在内存(RAM)中创建对象。
Is there a way to delete this object from RAM?
有没有办法从 RAM 中删除这个对象?
回答by Vidya
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 referenceon the algorithm in Ruby 2.0.
除了破解底层的 C 代码,没有。垃圾收集由运行时管理,因此您不必担心。这是关于 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.
一旦您不再引用内存中的对象,垃圾收集器就会开始工作。你应该没事。
回答by vgoff
The simple answer is, let the GC (garbage collector) do its job.
简单的答案是,让 GC(垃圾收集器)完成它的工作。
When you are ready to get rid of that reference, just do object = nil. And don't make reference to the object.
当您准备好摆脱该引用时,只需执行object = nil. 并且不要参考object.
The garbage collector will eventually collect that and clear the reference.
垃圾收集器最终会收集并清除引用。
(from ruby site)
=== Implementation from GC
------------------------------------------------------------------------------
GC.start -> nil
GC.start(full_mark: true, immediate_sweep: true) -> nil
------------------------------------------------------------------------------
Initiates garbage collection, unless manually disabled.
This method is defined with keyword arguments that default to true:
def GC.start(full_mark: true, immediate_sweep: true); end
Use full_mark: false to perform a minor GC. Use immediate_sweep: false to
defer sweeping (use lazy sweep).
Note: These keyword arguments are implementation and version dependent. They
are not guaranteed to be future-compatible, and may be ignored if the
underlying implementation does not support them.
回答by Todd A. Jacobs
Ruby Manages Garbage Collection Automatically
Ruby 自动管理垃圾收集
For the most part, Ruby handles garbage collection automatically. There are some edge cases, of course, but in the general case you should never have to worry about garbage collection in a typical Ruby application.
大多数情况下,Ruby 会自动处理垃圾收集。当然,有一些边缘情况,但在一般情况下,您永远不必担心典型 Ruby 应用程序中的垃圾收集。
Implementation details of garbage collection vary between versions of Ruby, but it exposes very few knobs to twiddle and for most purposes you don't need them. If you find yourself under memory pressure, you may want to re-evaluate your design decisions rather than trying to manage the symptomof excess memory consumption.
垃圾收集的实现细节因 Ruby 版本而异,但它公开的旋钮很少,并且在大多数情况下您不需要它们。如果您发现自己处于内存压力之下,您可能需要重新评估您的设计决策,而不是试图管理内存消耗过多的症状。
Manually Trigger Garbage Collection
手动触发垃圾回收
In general terms, Ruby marks objects for garbage collection when they go out of scope or are no longer referenced. However, some objects such as Symbolsnever get collected, and persist for the entire run-time of your program.
一般而言,当对象超出范围或不再被引用时,Ruby 会标记对象进行垃圾回收。但是,某些对象(例如Symbols)永远不会被收集,并且会在程序的整个运行时持续存在。
You can manually trigger garbage collection with GC#start, but can't really free blocks of memory the way you can with C programs from within Ruby. If you find yourself needing to do this, you may want to solve the underlying X/Y problem rather than trying to manage memory directly.
您可以使用GC#start手动触发垃圾收集,但不能像使用 Ruby 中的 C 程序那样真正释放内存块。如果您发现自己需要这样做,您可能希望解决潜在的 X/Y 问题,而不是尝试直接管理内存。
回答by Hepeng Zhang
You can't explicitly destroy object. Ruby has automatic memory management. Objects no longer referenced from anywhere are automatically collected by the garbage collector built in the interpreter.
您不能显式销毁对象。Ruby 具有自动内存管理功能。不再从任何地方引用的对象由解释器中内置的垃圾收集器自动收集。
Good article to read on how to do allocation wisely, and a few tools you can use to fine tune.
关于如何明智地进行分配的好文章,以及一些可用于微调的工具。
http://merbist.com/2010/07/29/object-allocation-why-you-should-care/
http://merbist.com/2010/07/29/object-allocation-why-you-should-care/

