Search code examples
flutterdartmobiledependency-injectionflutter-getx

What are the memory management differences between Get.put(SomeClass(), permanent: true) and Get.put(SomeGetXService())


I am using Getx and its dependency injection mechanism.

sometime I am overthinking - should I inject a class that should remain in memory (for good as a Singelton) using

Get.put(SomeClass(), permanent: true)

or using

Get.put(SomeGetXService())

by reading the documentation, both ways seems to put the class in memory as Singelton, and it can only be deleted explicitly (i.e. not with Get.smartManagement).

as for me, I prefer not to extend the class with GetxService, since the first option is simpler to implement - but I feel like I might be missing something. having the class in memory as Singelton through out the app life-span is a must. Thanks for your help


Solution

  • The first option always keeps the class in memory throughout the entire app's lifespan, while the second option binds the service to the stack navigation lifecycle. According to GetX dpcumentation, a GetXService is only deleted with a call to Get.reset(). In most cases though, both methods will probably be equally valid, since in essence, a service is kept in memory throughout the entire app lifecycle anyways.