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