# Objects support init() but missing finalize type function

**URL:** <https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225>\
**Category:** Support\
**Created:** [June 20, 2018, 5:31pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225 "2018-06-20T17:31:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlanaghen](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dlanaghen](https://discuss.kotlinlang.org/u/dlanaghen)\
**Post date:** [June 20, 2018, 5:31pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/1 "2018-06-20T17:31:58Z")

</div>

I love the lazy initialization of Kotlin singleton objects but I am not seeing any way to implement some kind of finalizer which is executed when the application shuts down so I can do some cleanup. Is this a language feature that is missing?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 20, 2018, 5:50pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/2 "2018-06-20T17:50:34Z")

</div>

You can use java’s finalize method: [https://kotlinlang.org/docs/reference/java-interop.html#finalize](https://kotlinlang.org/docs/reference/java-interop.html#finalize)

```kotlin
class Test {
    fun finalize(){ // dont use override and can't be private
        // do something
    }
}

```

This however is not guaranteed to be called at all. This will only get called when the garbage collector removes the object from memory. This might not happen if your program terminates and there is no control when this will execute, so I would advice against using this.

* * *

In general this is a limitation on how the JVM works. Due to the GC there is no way of knowing when an object actually gets finalized so I would not put any must run code here.

If you have any resources which have to be finalized and cause problems if you don’t you have to do this manually.

---

<div class="post-metadata">

**Author:** ![dlanaghen](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dlanaghen](https://discuss.kotlinlang.org/u/dlanaghen)\
**Post date:** [June 20, 2018, 5:59pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/3 "2018-06-20T17:59:56Z")

</div>

That may work for a class. I am specifically interested in a singleton object:

```
object A {
    init {
        println("initialized A")
    }

    wishThereWasAFinalize
    {

    }
}

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 20, 2018, 6:20pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/4 "2018-06-20T18:20:29Z")

</div>

As I said, this might not even work for a class and is a limitation of the JVM. As far as I know the only option is to use a manual system for that kind of stuff. You could for example have your singletons implement a interface and also add them to a list which you can then iterate through at the end of your program.

```kotlin
interface Finalized {
    fun finalize()
}
val objectsToFinalize = emptyMutableList<Finalized>()

object A: Finalized {
    init { objectToFinalize.add(this) }
}

fun main(args: Array<String>){
    // execute program
    
    objectsToFinalize.forEach { it.finalize() }
}

```

Otherwise, I think you should be able to implement `finalize` for objects the same way you do for classes. The only drawback is that the chance of the gc removing them at the end is near 0.

---

<div class="post-metadata">

**Author:** ![dlanaghen](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dlanaghen](https://discuss.kotlinlang.org/u/dlanaghen)\
**Post date:** [June 20, 2018, 6:28pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/5 "2018-06-20T18:28:31Z")

</div>

Interesting. I am looking at replacing a testing framework written Java/Spring and using Kotlin which looks to be more lightweight for what I need. It would be ideal if I could replace my Spring services with Kotlin objects and replace my @PostConstruct and @PreDestroy code with init{} and finalize{}.

Your proposed solution looks like it would meet my needs but obviously not quite as nifty as it would be if Kotlin and the JVM supporting a finalize function for singleton objects.

Thanks. I will head down this path.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 20, 2018, 6:48pm UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/6 "2018-06-20T18:48:46Z")

</div>

I mean if you need this often you could probably do most of the initialization using Kapt. Using the `ProcessingEnvironment` and `RoundEnvironment` maybe in combination of the kotlin metadata framework you should be able to iterate over all classes implementing the interface and create the list at compile time. That way you could ensure you don’t forget to add an object to the `objectsToFinalize`.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 21, 2018, 5:11am UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/7 "2018-06-21T05:11:35Z")

</div>

If you want to free up some kind of resources when the object is destroyed, it should implement `AutoCloseable` interface. It has a method `close` which is invoked either manually, or when object leaves scope. It is also a good practice to use it in combination with try with resources constructs (`use` extension in case of Kotlin).

I am not sure about singletons though. I think, it is considered bad architecture to allocate and free resources permanently in the singleton.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 21, 2018, 5:21am UTC](https://discuss.kotlinlang.org/t/objects-support-init-but-missing-finalize-type-function/8225/8 "2018-06-21T05:21:10Z")

</div>

I think that classes you propose have completely different lifecycles. Spring invokes the methods for construction and destruction of services whenever it needs. `init` method is invoked on singleton creation in other words on class load, which could not be controlled in any way.

Generally, you should not ever put anything but property configuration in `init` block. So if you have some kind of meaningful actions in initialization, you should still use some kind of `start/stop` methods and call the explicitly in your application lifecycle.
