Android process management and static variable garbage collection

Source: Internet
Author: User

1, the life cycle of the Android static variable

The life cycle of a static variable adheres to the Java design. We know that a static variable allocates memory when the class is load and exists in the method area. When the class is unloaded, the static variable is destroyed .

In a PC's client program, a class is loaded and unloaded, which can be simply equivalent to the start and end of the JVM process . What about Android? The same Dalvik VM is used. However, Android is not a very prominent process concept, so the life cycle of static variables will be blurred, this ambiguity for value types is irrelevant, if it is a static object reference , it is related to memory recovery, memory leaks , it is necessary to deepen research and understanding.

2. When will the process end in Android?

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. (PS: If the resource is tight, the system will kill the process, at which time the static variable reference will not be reclaimed by the garbage collector, causing a memory leak)

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.

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

Android process management and static variable garbage collection

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.