静态成员意见的活动 - 机器人静态、机器人、成员、意见

2023-09-12 05:58:17 作者:等无此人

在每一个在我的应用程序的活动,所有的意见(网格/列表/按钮...很多人)被声明为活动的静态成员。这是很好的做法还是有更好的办法。一直试图现在摸不着了一个月,终于决定去向。任何帮助AP preciated!

In each of the activities in my application, all the views (grids/lists/buttons... lots of them) are declared as static members of the activity. Is this good practice or is there a better way. Been trying to figure this for a month now and finally decided to ask. Any help appreciated!

推荐答案

HTTP: //developer.android.com/training/articles/perf-tips.html 。 检查以了解什么时候使用静态变量的文档。

http://developer.android.com/training/articles/perf-tips.html. Check the documentation to understand when to use static variables.

我同意Boardy的评论。为什么你需要你的UI元素是静态的。

I agree with Boardy's comment. Why do you need your ui elements to be static.

使用静态变量不推荐使用。静态变量存储在堆的一个的PermGen部分。即使类完成它的工作原理静态变量停留在堆。垃圾回收器标记和清除。如果具有其中设置在第二活动所引用的第一活性的静态变量,该链接保持长。你可能会,如果你使用更多的静态变量获取内存泄漏。同时减少使用不必要的对象。

Using static variables is not recommended. Static variables are stored in a PermGen section of the heap. Even if the class finishes it works the static variables stays in the heap. The garbage collector does mark and sweep. If you have a static variables in the 1st activity which is referenced in the 2nd activity, the link stays long. You might get memory leaks if your using more static variables. Also reduce use of unnecessary objects.

静态变量是由这是由类加载器参考 - 所以,除非要么下降的ClassLoader类以某种方式或类加载器本身就符合静态变量将不会被垃圾收集垃圾收集类对象引用。所以,如果你使用的是在多类引用这些类和在其中的静态变量的声明不能被垃圾收集,除非这些类可用于垃圾收集静态变量。因此,这将导致堆内存扩展,导致内存泄漏。

Static variables are referenced by Class objects which are referenced by ClassLoaders -so unless either the ClassLoader drops the Class somehow or the ClassLoader itself becomes eligible for garbage collection the static variables won't be garbage collected. So if you are using static variables referenced in many class those classes and the one in which static variables are declared cannot be garbage collected unless those classes are available for garbage collection. So this leads to heap memory expanding leading to memory leaks.

在这个视频为什么静态变量不应使用以及如何避免内存泄漏的家伙会谈。的 http://www.youtube.com/watch?v=_CruQY55HOk 。有关使用MAT分析仪来检查内存泄漏的人谈判。

In this video the guy talks about why static variables should not be used and how to avoid memory leaks.http://www.youtube.com/watch?v=_CruQY55HOk. The guy talks about using MAT Analyzer to check for memory leaks.

也看看这个链接。 http://developer.android.com/guide/faq/framework.html.Have 看看细节的标题下*活动/服务之间如何传递数据在一个单一的应用程序?

Also have a look at this link. http://developer.android.com/guide/faq/framework.html.Have a look at the details under the heading *How do I pass data between Activities/Services within a single application?.