Crystal lang, is it possible to explicitly dispose (for free) an instance (object) without waiting for the GC?

The title says it all. Maybe the method can be called how def destruct; delete self;end?

+4
source share
1 answer

It is possible, but definitely not recommended, and as I show you, it may change or break in the future. Why do you need this? The GC idea is definitely not worried about such things.

class Foo
  def initialize
    @x = 10
  end

  def finalize
    puts "Never called"
  end
end

foo = Foo.new
p foo # => #<Foo:0x10be27fd0 @x=10>
GC.free(Pointer(Void).new(foo.object_id)) # This line frees the memory
p foo # => #<Foo:0x10be27fd0 @x=1>
+5
source

Source: https://habr.com/ru/post/1627731/


All Articles