How to destroy Ruby object?

前端 未结 4 1974
有刺的猬
有刺的猬 2020-12-05 13:46

Suppose there is simple object like:

object = Object.new

As I know this creates Object in memory (RAM).

Is there a way to delete th

相关标签:
4条回答
  • 2020-12-05 14:17

    Ruby Manages Garbage Collection Automatically

    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.

    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 symptom of excess memory consumption.

    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 Symbols never get collected, and persist for the entire run-time of your program.

    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.

    0 讨论(0)
  • 2020-12-05 14:29

    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.

    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/

    0 讨论(0)
  • 2020-12-05 14:34

    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.

    0 讨论(0)
  • 2020-12-05 14:41

    The simple answer is, let the GC (garbage collector) do its job.

    When you are ready to get rid of that reference, just do object = nil. And don't make reference to the 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.
    
    0 讨论(0)
提交回复
热议问题