How to destroy Ruby object?

前端 未结 4 1998
有刺的猬
有刺的猬 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: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.
    

提交回复
热议问题