I am new to Kotlin and just caught a situation, is there any concept or functionality that if a class have at lease one instance one variable defined as Static (or something) have presence in memory and if that last instance is collected by garbage collector that static field will be removed as well.
Asked
Active
Viewed 46 times
0
-
Not sure if I understood your usecase, can you put an example? Maybe you can play around with [PhantomReference](https://docs.oracle.com/javase/9/docs/api/java/lang/ref/PhantomReference.html)? – Alberto S. Sep 25 '20 at 07:50
-
Are you asking if static (aka companinon object members) get collected once there are no more instances of that class? The answer is `no`. The companion object is its own class which exists independiently of instances of the class it the companion of. Java gives you exotic ways of unloading a class which should clean the static members as well. https://stackoverflow.com/questions/148681/unloading-classes-in-java – al3c Sep 25 '20 at 13:10