A brief introduction to the lifecycle of Android static variables _android

Source: Internet
Author: User
Tags garbage collection

Android is developed in Java, and its static variable lifecycle follows Java design. We know that static variables are allocated in memory when the class is load, and exist in the method area. When the class is unloaded, the static variable is destroyed. In the PC client program, a class is loaded and unloaded, which is simply equivalent to the start and end of the JVM process. So what's in Android? The same is true with the Dalvik VM. However, Android is not very prominent in the process concept, so the static variable life cycle will feel blurred, this kind of ambiguity for the value type is indifferent, if it is static object reference, and memory recycling, memory leaks These problems, it is necessary to deepen research and understanding.

A static variable allocates memory when the class is loaded.

When is the class loaded?

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

Second, static variables are destroyed when the class is unloaded.

When is the class unloaded?

At the end of the process.

Note: In general, all classes are loaded by default ClassLoader, as long as ClassLoader exists, classes are not unloaded, and the default ClassLoader lifecycle is consistent with the process, this article discusses the general situation.

Iii. when does the process of Android end

This is the core of Android, which is different from the PC for process and memory management-if the resources are adequate, Android will not kill any process, another means that the process can be killed at any time. And Android will restart the process of being killed when resources are enough. In other words, the value of static variables, if not done processing, is unreliable, it can be said that everything in memory is unreliable. If you want to be reliable, you have to save it to a NAND or SD card and return it when you reboot.

Another situation is that you cannot leave all activity equivalent to the exit of the process, so when the user clicks on the icon to start the application, previously stored in the static variable value, it may still exist, so to the specific circumstances to give the empty operation.

Four, application is also the same unreliable

Application is actually a single example object, but also put in memory, when the process is killed, it is all clear, but the Android system will help rebuild application, and we stored in the application of the data is naturally not, or have to deal with themselves.

Five, statically referenced objects are not garbage collected

As long as the static variable is not destroyed and null is not set, its object is always referenced and the reference count cannot be 0, so it is not garbage collected. Therefore, a single instance object will not be reclaimed at run time

Related Article

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.