2

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!

4

4 に答える 4

3

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

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

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.

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.

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.

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

于 2013-03-25T11:54:39.097 に答える
2

this two link give you ans of your question :

one

Two

also detail of application performance.

于 2013-03-25T11:52:41.407 に答える
1

Personally I'd avoid using static variables as these variables are kept within memory, even though the activity may no longer require them.

The UI components don't really need to be static as they should be retrieved from the onCreate using the findViewById, or created programatically in the onCreate and there is no need to store the variable permanently.

于 2013-03-25T11:51:19.320 に答える
1

You should not qualify your view variables in your Activity class as static, since a static variable is for the class rather than for your Activity instance:

Public MyActivity extends Activity {
    private static final string TAG = MyActivity.class.getSimpleName(); // This is a good candidate for `static`.
    private static Switch myFirstSwitch; // This is a bad candidate for `static`
    private Switch mySecondSwitch; // This is good, and can be accessed by onCreate(), onResume(), etc.
    // …
}

If you have not encountered issues so far while using myFirstSwitch, it is because in practice Android instantiates a single MyActivity instance when parsing file AndroidManifest.xml. So at the application level there is no significant difference between using static variables and not using them, although internally static variables have to be handled differently.

However, in theory, you could instantiate MyActivity multiple times in your code by yourself. For example, for some reasons or just for testing purposes, you might write somewhere MyActivity testActivity = new MyActivity(); In this case you might be in trouble using the static myFirstSwitch variable.

于 2016-05-06T17:59:52.000 に答える