gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:22:45 25 4
gpt4 key购买 nike

所以我在有关内存性能的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com