请问"避免依赖注入框架" Android的内存指南适用于匕首呢?适用于、匕首、框架、内存

2023-09-04 23:05:13 作者:可念不可说

所以,我所遇到的在Android这条最佳实践的内存性能。

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

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

他们说

避免依赖注入框架

使用依赖注入框架如吉斯或RoboGuice可能   是有吸引力的,因为它们可以简化你写的code和提供   自适应环境的测试和其他有用的   配置更改。然而,这些框架往往执行大量   进程初始化扫描您的code的注释中,这   可以要求被映射到内存显著金额的code   即使你并不需要它。这些映射的页面被分配到   清洁内存,使机器人可以删除它们,但是这不会发生,直到   页已经被留在内存中的很长一段时间。

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.

卖什么了解匕首他们声称要快。不知道哪一个我应该去的?

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

..框架[像吉斯工作]倾向于的执行很多进程初始化的扫描你的$ C $下的注释的,可以的需要显著的金额$ C $的C到被映射到内存的,即使你不需要它。

..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框架的的不的扫描说[运行时]注释,寓意[过度]利用反射原理,那么这个理由并不适用。虽然匕首不使用标注这些的使用的不同的的比吉斯 1 和避免问题说明。

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.

由于匕首被写为A的快速的依赖注入的 Android版的和Java,作者设计了它用于此目的,并认为它适合于这样的目标 - 勇往直前,给它一个尝试

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 匕首使用了编译时的注解(当然,mostly)而不是依赖于运行时的注解和反思;它是运行时的注解扫描和反射,导致内存指南是警告有关问题

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.