When is Scala going to trash?

This question relates to Scala objects, which are defined something like this:

object Pipeline {
   val dispatcher = new Dispatcher
}

I know that in some cases they are called โ€œcompanionโ€ objects, although I still do not understand the difference. Anyway, I want to know when they collect garbage. So, in the above example, I would like to know when the memory occupied dispatcherwill be restored, if ever.

+3
source share
3 answers

jave - lazyily . Java , , , , . .

+10

Pipeline "", ( Java), , . , -

object Pipeline {
    var dispatcher = Some(new Dispatcher)
    def close() { dispatcher = None }
}
+6

, , . , , dispatcher , (, ).

In addition to manually resetting the field dispatcherto make the corresponding object dispatcheravailable for collection, you can always automate this process using SoftReference.

+3
source

Source: https://habr.com/ru/post/1770041/


All Articles