Android try not to use static variables

Source: Internet
Author: User

Potential problems with static statics static variables:

1. Memory is occupied and memory is not normally released;

2. Static memory is automatically reclaimed when the system is not in memory, which can cause access to global static errors.

3. The activity cannot be used as a static statically object, so that all component objects of the activity are stored in global memory and are not recycled;

(Transferred from: http://blog.csdn.net/ctcwri/article/details/8858414)

The life cycle of a static variable:

When is the class loaded?

When we launch an app, the system creates a process that loads an instance of the Dalvik VM, then the code runs on the DVM, the load and unload of classes, garbage collection, and so on are the responsibility of the DVM. This means that when the process starts, the class is loaded and the static variable is allocated memory.

Static variables are destroyed when the class is unloaded.

When was the class unloaded?

At the end of the process.

Note: In general, all classes are default ClassLoader loaded, as long as ClassLoader exist, the class will not be unloaded, and the default ClassLoader life cycle is consistent with the process, this article discusses the general situation.

Iii. when the process of Android is over

This is Android's process and memory management is different from the core of the PC-if the resources are sufficient, Android will not kill any process, another means that the process can be killed at any time . Android will restart the process of being killed when resources are sufficient. That is, the value of a static variable, if not done, is unreliable, it can be said that everything in memory is unreliable. If you want to be reliable, you still have to save it to the NAND or SD card and restore it back when you reboot.

Another scenario is that you cannot leave all activity equal to the exit of the process, so when the user clicks on the icon to launch the application, the value that was previously stored in the static variable may still exist , so it is necessary to give the empty operation depending on the situation.

Four, application is equally unreliable

Application is actually a singleton object, also put in memory, when the process is killed, it is completely emptied, but the Android system will help rebuild the application, and we stored in the application data naturally no, or have to deal with their own.

V. Objects that are statically referenced are not garbage collected

As long as the static variable is not destroyed and there is no null, its object has been kept referenced, that is, the reference count cannot be 0, and therefore is not garbage collected. Therefore, singleton objects are not recycled at run time.

Extension: http://blog.csdn.net/u010192087/article/details/41206251

Android try not to use static variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.