# When is #finalize run?

**URL:** <https://forum.crystal-lang.org/t/when-is-finalize-run/390>\
**Category:** Help & Support\
**Created:** [February 7, 2019, 2:25pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390 "2019-02-07T14:25:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 7, 2019, 2:25pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/1 "2019-02-07T14:25:01Z")

</div>

I wrote up this example, to see if a finalizer goes away when a class is inherited, but the finalizer doesn’t seem to be called at any point. When is a finalize method called?

```auto
class Parent
  property parent_prop = "Parent property"

  def initialize
    puts "initializing parent"
  end
  def initialize(@parent_prop)
  	puts "initializing parent"
  end
	def finalize
    puts "Finalizing parent!"
  end
end

class Child < Parent
  property child_prop = "Child property"
  
  def initialize
    puts "initializing child"
  end
end

p! (p = Parent.new), (p = nil), (c = Child.new), (c = nil)

sleep 15
puts "collecting"
100.times { GC.collect }
puts "done"

```

output:

```auto
(p = Parent.new) # => initializing parent
#<Parent:0x102d20f20 @parent_prop="Parent property">
(p = nil) # => nil
(c = Child.new) # => initializing child
#<Child:0x102d20f00 @parent_prop="Parent property", @child_prop="Child property">
(c = nil) # => nil
collecting
done

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 7, 2019, 2:56pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/2 "2019-02-07T14:56:27Z")

</div>

Try this:

```auto
p = Parent.new
p = nil
c = Child.new
c = nil

puts "collecting"
GC.collect
puts "done"

```

The `p!` macro creates temporary variables to hold each of the expressions. That means they will still be in the stack when you call `GC.collect`.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 7, 2019, 2:58pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/3 "2019-02-07T14:58:34Z")

</div>

Actually, what I said is not correct… but I’m sure some things remain in the stack, for some reason. I’ll need to debug it, but I don’t consider this something super important. The GC will collect things when they are unreachable: no references in the heap or stack. And you shouldn’t rely on `GC.collect` to collect things you don’t need anymore.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 7, 2019, 7:33pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/4 "2019-02-07T19:33:35Z")

</div>

My use of GC.collect was just to try to get the `#finalize` method to be called. I suppose my real question is whether the `#finalize` method of a superclass goes away along with all of the initialize methods when an initialize method is overridden in a subclass, or if perhaps a `#finalize` method is never inherited… some strange behavior like that.

To be specific, I’m writing a shard and CLI for converting between different subtitle file-formats. I wanted a superclass `Format` with a `@content` property which could be any IO including an open file, and I wanted to make sure that this method would make it into the subclasses and not just be a part of the (abstract) superclass.

```crystal
def finalize
  @content.close if @content.responds_to? :close
end

```

Thanks @asterite

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 7, 2019, 7:50pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/5 "2019-02-07T19:50:11Z")

</div>

Yes, `finalize` is (or should) be inherited.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 7, 2019, 7:50pm UTC](https://forum.crystal-lang.org/t/when-is-finalize-run/390/6 "2019-02-07T19:50:39Z")

</div>

Thank you!
