SoFunction
Updated on 2025-03-11

A brief discussion on the use of static variables in Android

In the project, after continuously receiving serial port data for a long time (a few hours), an error will be reported by chance. The tension problem of Oom and CPU was initially ruled out, because it is not convenient for industrial tablets to debug, so I used some relatively stupid methods to finally lock them in several places where static was used. Here are some issues with using static in Android.

The life cycle of static variables complies with Java's design. Static variables allocate memory when the class is loaded and exist in the method area. When the class is unloaded, the static variable is destroyed. In the client program of the PC, a class is loaded and unloaded, which can simply be equivalent to the start and end of the jvm process. In Android, the same is true for DVMs, but the concept of process is not very prominent in Android, so the life cycle of static variables is fuzzy. This kind of fuzzy does not matter to value types. If it is a static object reference, it is related to problems such as memory recycling and memory leakage, and it is necessary to deepen research and understanding.

1. Static variables allocate memory when the class is loaded.

When is the class loaded?

When we start an app, the system will create a process, which will load a DVM instance, and then the code will run on the DVM. The DVM will be responsible for class loading, unloading, garbage collection and other matters. That is to say, when the process starts, the class is loaded and the static variable is allocated memory.

2. Static variables are destroyed when the class is unloaded.

When is the class uninstalled?

At the end of the process.

Note: Generally speaking, all classes are loaded by the default ClassLoader. As long as the ClassLoader exists, the class will not be unloaded. The default ClassLoader life cycle is consistent with the process. The general situation is discussed here.

3. When will the Android process end?

This is the core of Android's process and memory management that is different from that of PC--If there are enough resources, Android will not kill any process. Another meaning is that the process may be killed at any time. Android will restart the killed process when there are enough resources. In other words, the value of a static variable is unreliable if it is not processed. It can be said that everything in memory is unreliable. If it is to be reliable, it is better to save it to a Nand or SD card and restore it during restart.

Another situation is that exiting all activities cannot be equated with exiting the process. Therefore, when the user clicks the icon to start the application, the values ​​previously stored in static variables may still exist, so the clear operation should be given depending on the specific situation.

4. Application is also unreliable

Application is actually a singleton object, and it is also placed in memory. When the process is killed, it is all cleared. However, the Android system will help rebuild the Application, and the data stored in the Application will naturally be gone, so we still have to handle it ourselves.

5. Will statically referenced objects be recycled?

In a Java runtime environment other than Android, you only need to care about the process lifecycle, and you can use static variables to maintain data with confidence during the process lifecycle. As long as the static variable is not destroyed or nulled, its objects are kept referenced and therefore will not be garbage collected. Therefore, singleton objects will not be recycled at runtime. However, in Android, it will be NULLed by the system at any time.

Summarize:

In Android, we don't know when the process will be killed, all:

1. There is no guarantee that static variables will always exist. (The process may be Killed)

2. Every time the app is opened, the value of the static variable is the initial value (the process has not been killed by all static variables or the value of the last time).

Static variables will not be garbage collected, and their objects are always referenced, and ARC cannot be 0.

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!