Skip to content

Question: Memory Allocation and Performance of using Scopes #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
creativepsyco opened this issue Jun 16, 2021 · 5 comments
Closed

Question: Memory Allocation and Performance of using Scopes #205

creativepsyco opened this issue Jun 16, 2021 · 5 comments

Comments

@creativepsyco
Copy link

Hi,

I wanted to get a deeper understanding of what happens when popScope is called.

Assume that we have registered the following:

// Assume that Account has a `dispose()` contract
abstract class  Account implements Disposable {}

class NoAccount extends Account {}

class LoggedInAccount extends Account {}

// Configuration
getIt.registerSingleton<Account>(NoAccount('no_account'));
getIt.pushNewScope();

getIt.registerSingleton<Account>(LoggedInAccount('account_1'));
await getIt.popScope();

Will at the last line popScope() does it end up calling dispose() on the dependency graph and registered instances/singletons? Or would they be lying around in memory even if the scope got disposed?

@escamoteur
Copy link
Collaborator

if your class implements Disposable its dispose() function will get called when the scope where it is registered in gets popped or if you unregister it manually. Is this unclear in the readme? If yes, please tell me how to improve it.

@escamoteur
Copy link
Collaborator

Uhm, what do you mean by Dependency Graph? you might also check out the ShadowChangeHandlers for scenarios like the one you show above with differnt implementations of the same base class in different scopes

@creativepsyco
Copy link
Author

I didn't see examples of Disposable or dispose being used in the Readme. I saw the disposingFunction in action but thats a separate argument that needs to be passed in during registration.

In the above example, its simple because there is only 1 type being referenced, but there could be other multiple objects depending on each other, and deregistering of one would lead to deregistering of others, kind of like a cascade or garbage collection.

e.g. if LoggedInAccount had n dependencies and assuming those dependencies themselves don't have any other dependencies, would those still remain in memory or completely removed from getIt? Would it make a difference if those objects were lazySingletons and only constructed because LoggedInAccount had to be created.

I was mostly looking at the implementation of the Scope class to gain an understanding of where the disposing might happen.

Seems like _ServiceFactory.dispose() is called which in turn calls the disposingFunc.call (instance as T) so I suppose the dispose will end up getting invoked, but instead I need to write

getIt.registerSingleton<Account>(NoAccount('no_account'), disposingFunction: (t) => t.dispose());

Is my understanding correct?

@escamoteur
Copy link
Collaborator

image
Here you see the line that takes care that the dispose method of your class is called.

If your class does not implement Disposable

getIt.registerSingleton<Account>(NoAccount('no_account'), disposingFunction: (t) => t.dispose());

is an alternative option so register a disposing function although I find implementing Disposable the more elegant way.

GetIt doesn't know which other objects are referenced from your Objects, so it cant call automatically a dispose function there.
But typically you will register all classes that need to be disposed together within the same Scope or a child scope. By using popUntil you can pop multiple Scopes with one function call.

@creativepsyco
Copy link
Author

Oh I was looking at master branch, I saw this exists in the nullsafety branch. Thanks that clarifies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants