I am trying to understand the lifecycle of constructor parameters in Kotlin.
Given the following code, when will FooFactory class instance be available for collection by garbage collector?
Since the usage of FooFactory is delayed, will the runtime know when to release it?
Or will this create a leak if the compiler does not know when to release FooFactory and it will live forever?
class Foo {
fun doStuff() {}
}
class FooFactory {
fun getFoo() : Foo {
return Foo()
}
}
class User(factory: FooFactory) {
val x: Foo by lazy {
factory.getFoo()
}
}
val user = User(FooFactory())
Thread.sleep(100)
user.x.doStuff()
Thread.sleep(100)