Crystal lang, is it possible to explicitly dispose (free) of instance (object) without waiting for GC?
Asked Answered
C

1

6

The title says it all. Maybe there's a method can be called like def destruct; delete self;end?

Canoness answered 8/2, 2016 at 6:14 Comment(0)
W
7

It's possible, but definitely not recommended and the way I'll show you might change or break in the future. Why do you need this? The idea of a GC is precisely not worrying 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>
Weisburgh answered 8/2, 2016 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.