How to destroy Ruby object?

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

提交回复
热议问题