# Question about finalize

**URL:** <https://forum.crystal-lang.org/t/question-about-finalize/6289>\
**Category:** Help & Support\
**Created:** [January 10, 2024, 8:46am UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289 "2024-01-10T08:46:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![femto](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/femto/32/2246_2.png) [@femto](https://forum.crystal-lang.org/u/femto)\
**Post date:** [January 10, 2024, 8:46am UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/1 "2024-01-10T08:46:15Z")

</div>

```auto
class Foo
  
  def finalize
      puts "finalize"
  end
end
Foo.new

```

didn’t output anything  
I tried add `GC.collect`, still nothing,  
seemed finalize doesn’t get called?

Also, does struct has finalize?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 10, 2024, 2:10pm UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/2 "2024-01-10T14:10:37Z")

</div>

Ultimately it just means that the GC doesn’t want/need to garbage collect that instance in this contrived example. Structs aren’t allocated by the GC so no.

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [January 10, 2024, 4:16pm UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/3 "2024-01-10T16:16:25Z")

</div>

```cr
class Mortals
  def finalize
      puts "i am #{object_id}. My life is beautiful. Goodbye!"
  end
end

100000.times do
  Foo.new
end

```

```auto
crystal build m.cr

```

```auto
./m | wc -l

```

```auto
for i in {1..5}; do ./g | wc -l; done

```

```auto
99600
99738
97157
99547
96646

```

It seems that in some cases the end of the world will come while we are still alive!

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [January 10, 2024, 7:09pm UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/4 "2024-01-10T19:09:15Z")

</div>

This happens because the GC doesn’t run at program exit - if there is memory still allocated then the system is responsible for deallocating stuff, and the finalizer won’t get invoked. Finalizers should basically only be used in code dealing with C extensions that need them to help with cleaning up stuff in long running programs.

Personally I wish finalizers were run as part of shutting down, but there are a lot of potential issues around that that makes can be really hard to deal with.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 10, 2024, 7:13pm UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/5 "2024-01-10T19:13:27Z")

</div>

Related: [Finalize objects on program exit · Issue #13995 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/13995)

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 11, 2024, 3:17am UTC](https://forum.crystal-lang.org/t/question-about-finalize/6289/6 "2024-01-11T03:17:22Z")

</div>

Amos has some good advice for what happens when your program exits

 ![Well, everyone leaves unfinished business — quote from Amos Burton in the Expanse](https://canada1.discourse-cdn.com/flex036/uploads/crystal_lang/original/2X/f/faa70a7f7b3c79e6af7e50b57bcb0ccb414046d8.jpeg)
