koin icon indicating copy to clipboard operation
koin copied to clipboard

Feature Request: Allow instances to be reused, but also garbage collected

Open ankushg opened this issue 3 years ago • 8 comments

It's great that we have some flexibility between factory and singleton!

At the same time, we have many classes that could be reused if they already exist, without a strong need to only have one single instance.

  • With singleton, we find ourselves wasting memory by retaining such classes far beyond when we need them
  • With factory, we find ourselves wasting memory by creating too many simultaneous instances of classes that could have be shared

It would be great if we could tell Koin to

  • reuse an existing instance if it already has one, but also
  • give Koin the freedom to let that instance be garbage collected whenever it's no longer needed and recreated in the future

This is similar to Dagger's @Reusable scope.

ankushg avatar Jul 07 '22 20:07 ankushg

Yep, something like with weak references to garbage it if nobody has nay reference on it 🤔

arnaudgiuliani avatar Aug 26 '22 14:08 arnaudgiuliani

Any plans on when this will get added? It's been 1y+

Swinject already has this feature as well and I believe it is a must-have, otherwise we would need to be hacking around scopes.

lcszc avatar Oct 03 '23 12:10 lcszc

yes, it's something that need a bit of rework for internal engine

arnaudgiuliani avatar Oct 31 '23 06:10 arnaudgiuliani