On the use of static variables in Android

In the project, after continuously receiving serial port data for a long time (several hours), an error will be reported occasionally. The shortage of oom and CPU was initially eliminated, because it was an industrial tablet that was inconvenient to debug. Some stupid methods were used, and finally locked in several places where static was used. Here are some problems with using static in Android.

The life cycle of static variables follows the design of Java. Static variables allocate memory when the class is loaded and exist in the method area. When the class is unloaded, the static variables are destroyed. In the client program of PC, a class is loaded and unloaded, which can be simply equivalent to the start and end of JVM process. In Android, the DVM used is the same, but Android does not highlight the concept of process, so it feels vague about the life cycle of static variables. This ambiguity does not matter for value types. If it is a static object reference, it is related to memory recycling and memory disclosure, so 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. DVM is responsible for class loading and unloading, garbage collection and so on. That is, when the process starts, classes are loaded and static variables are allocated memory.

2、 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 the default classloader. As long as the classloader exists, the class will not be unloaded, and 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, which 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 you want to be reliable, you should save it to NAND or SD card and recover it when you restart.

Another situation is that quitting all activities cannot be equal to quitting the process. Therefore, when the user clicks the icon to start the application, the value previously stored in the static variable may still exist. Therefore, the emptying operation should be given according to the specific situation.

4、 Application is equally unreliable

Application is actually a singleton object, which is also placed in memory. When the process is killed, it will be completely cleared, but the Android system will help rebuild the application, and the data stored in the application will naturally be gone, so we have to deal with it ourselves.

5、 Statically referenced objects will not be recycled?

In the Java running environment other than Android, as long as you care about the process life cycle, you can safely use static variables to maintain data during the process life cycle. As long as the static variable is not destroyed or set to null, its object is always referenced, so it will not be garbage collected. Therefore, singleton objects are not recycled at run time. But in Android, it will be set to null by the system at any time.

Summary:

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. Each time the app is opened, the value of the static variable is the initial value (the process has not been killed, and all the static variables are still saved with the last value).

Static variables are not garbage collected, and their objects remain referenced. Arc cannot be 0.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>