Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dagger 2 has any benefit for performance? [closed]

Tags:

android

dagger

I'm a newbie in dagger 2 and dependence injection in android. I'm hearing that a lot of android developers use dagger 2. I understand that It manages dependencies between classes and with it we will not use anymore the keyword "new" but I want to know why should I use it really? Does it manage memory allocation and minimize leaks? Does it benefit the performance of the application?

like image 994
nimmp Avatar asked Nov 02 '17 16:11

nimmp


People also ask

What are the benefits of dagger?

Benefits of using Dagger Dagger frees you from writing tedious and error-prone boilerplate code by: Generating the AppContainer code (application graph) that you manually implemented in the manual DI section. Creating factories for the classes available in the application graph.

What is the use of dagger 2?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is the difference between dagger and dagger 2?

x: It is an adaptation of an earlier version created by Square and now maintained by Google. Dagger2 is compile time Dependency injection framework that generates code to connect dependencies at compile time.

Is dagger faster than Guice?

I know that dagger creates injection at compile time by generating code and hence its performance is better than Guice, which do it at runtime.


Video Answer


2 Answers

I want to concentrate on memory and performance of the application with dependency injection. So, I found this answer in android developer website. https://developer.android.com/topic/performance/memory.html#DependencyInjection :

Use Dagger 2 for dependency injection frameworks

can simplify the code you write and provide an adaptive environment that's useful for testing and other configuration changes.

If you intend to use a dependency injection framework in your app, consider using Dagger 2. Dagger does not use reflection to scan your app's code. Dagger's static, compile-time implementation means that it can be used in Android apps without needless runtime cost or memory usage.

Other dependency injection frameworks that use reflection tend to initialize processes by scanning your code for annotations. This process can require significantly more CPU cycles and RAM, and can cause a noticeable lag when the app launches.

like image 57
nimmp Avatar answered Oct 03 '22 23:10

nimmp


Performance is up to developer to avoid inefficient use of resources, such as the CPU, memory, graphics, network, and device battery. But dependence injection is a design pattern that is based on the concept of Inversion of Control which says that a class should get its dependencies from outside. In simple words, no class should instantiate another class but should get the instances from a configuration class instead of constructing them internally. So, this pattern allows developers to write code that has low coupling and which can therefore be easily tested and maintained.

But to compare the Performance of Dependency Injection Libraries , see this good link : http://blog.nimbledroid.com/2016/03/07/performance-of-dependency-injection-libraries.html

like image 43
Imene Noomene Avatar answered Oct 04 '22 00:10

Imene Noomene