android - Android 内存指南中的 "Avoid dependency injection frameworks"是否也适用于 Dagger?

标签 android performance dependency-injection dagger

所以我在有关内存性能的 Android 文章中看到了这个最佳实践。

http://developer.android.com/training/articles/memory.html

他们说

Avoid dependency injection frameworks

Using a dependency injection framework such as Guice or RoboGuice may be attractive because they can simplify the code you write and provide an adaptive environment that's useful for testing and other configuration changes. However, these frameworks tend to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don't need it. These mapped pages are allocated into clean memory so Android can drop them, but that won't happen until the pages have been left in memory for a long period of time.

但是 Dagger 呢?他们声称速度很快。不确定我应该去哪一个?

最佳答案

此建议同样适用于所有依赖注入(inject)框架。

..frameworks [that work like Guice] tend to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don't need it..

因此,如果使用扫描所述[运行时]注释的DI/IoC框架,暗示[过度]使用反射,那么这个理由不适用。而Dagger确实使用注释这些是 used differently而不是 Guice1 并避免上述问题。

由于 Dagger 被编写为“一个 fast 依赖注入(inject)器for Android and Java”,作者为此设计了它,并认为它适合这样的目标 - 来吧,试一试。


1 Dagger 使用编译时注解(嗯,mostly)而不是依赖运行时注解和反射;正是运行时注释扫描和反射导致了内存指南警告的问题。

关于android - Android 内存指南中的 "Avoid dependency injection frameworks"是否也适用于 Dagger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24194283/

相关文章:

java - hibernate是否默认使用PreparedStatement

javascript - 在 JavaScript 中生成大量独特的小随机数的最快方法

dependency-injection - DI : Composition root decomposition

java - 火力 : get data details from another child

android - OSGi(费利克斯): Change standard packages of system bundle to make it run on Android

android - 在一个通用池 AndEngine 中使用不同的 Sprite 纹理

android - 自动滚动图库小部件

r - 数据框到嵌套列表

java - Dagger 2在提供方法中使用命名注释

java - 通过拦截器或监听器或预处理器覆盖特定的 guice 绑定(bind)