是否“避免依赖注入框架"?在 Android Memory Guide 中也适用于 Dagger? [英] Does "Avoid dependency injection frameworks" in the Android Memory Guide apply to Dagger as well?

查看:22
本文介绍了是否“避免依赖注入框架"?在 Android Memory Guide 中也适用于 Dagger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在关于内存性能的 Android 文章中遇到了这个最佳实践.

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

他们说

<块引用>

避免依赖注入框架

使用 Guice 或 RoboGuice 等依赖注入框架可能有吸引力,因为它们可以简化您编写和提供的代码一个对测试和其他有用的适应性环境配置更改.然而,这些框架往往执行很多通过扫描代码中的注释来初始化进程,可能需要将大量代码映射到 RAM即使你不需要它.这些映射页被分配到清理内存,以便 Android 可以删除它们,但这不会发生,直到页面已在内存中保留很长时间.

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

解决方案

此建议并非同样适用于所有依赖注入框架.

<块引用>

..frameworks [像 Guice 一样工作] 倾向于通过扫描代码以获取注释来执行大量进程初始化,这可能需要将大量代码映射到 RAM 中 即使你不需要它..

因此,如果使用扫描所述[运行时]注释的DI/IoC框架,暗示[过度]使用反射,那么这个原因不适用.虽然 Dagger 确实使用了注释,但这些是 使用与Guice1不同并避免所述问题.

由于 Dagger 被编写为一个 快速 依赖注入器Android 和 Java",作者为此目的而设计它并相信它适合这样的目标 - 继续,试一试.

<小时>

1 Dagger 使用编译时注释(好吧,主要) 而不是依赖运行时注释和反射;导致内存指南警告的问题是运行时注释扫描和反射.

So I have come across this best practices on Android articles on memory performance.

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

They said

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.

But what about Dagger which they claim to be fast. Not sure which one should I go for?

解决方案

This recommendation does not apply equally to all dependency injection frameworks.

..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..

Thus, if using a DI/IoC framework that doesn't scan for said [run-time] annotations, implying the [excessive] use of reflection, then this reason doesn't apply. While Dagger does use annotations these are used differently than by Guice1 and avoid the problem stated.

Since Dagger was written as "A fast dependency injector for Android and Java", the authors have designed it for this purpose and believe that it is suitable for such a target - go ahead, give it a try.


1 Dagger uses compile-time annotations (well, mostly) instead of relying on run-time annotations and reflection; it is the run-time annotation scanning and reflection that causes the issue the memory guide was warning about.

这篇关于是否“避免依赖注入框架"?在 Android Memory Guide 中也适用于 Dagger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆